# --2-Vectors

&#x20;\#1-Create a vector

```r
# Define the variable vegas
vegas <- "Go!"
```

&#x20;\#2-Create a vector (2)

```r
numeric_vector <- c(1, 10, 49)
character_vector <- c("a", "b", "c")
​
# Complete the code for boolean_vector
boolean_vector <-c(TRUE,FALSE,TRUE)
```

&#x20;\#3-Create a vector (3)

```r
#Assign the winnings/losses for roulette to the variable roulette_vector
​
# Poker winnings from Monday to Friday
poker_vector <- c(140, -50, 20, -120, 240)
  
# Roulette winnings from Monday to Friday
roulette_vector <-c(-24,-50,100,-350,10)
```

&#x20;\#4-Naming a vector

```r
# Poker winnings from Monday to Friday
poker_vector <- c(140, -50, 20, -120, 240)
  
# Roulette winnings from Monday to Friday
roulette_vector <- c(-24, -50, 100, -350, 10)
  
# Assign days as names of poker_vector
names(poker_vector) <- c("Monday", "Tuesday", "Wednesday", 
"Thursday", "Friday")
  
# Assign days as names of roulette_vectors
names(roulette_vector) <- c("Monday", "Tuesday", 
"Wednesday", "Thursday", "Friday")
```

&#x20;\#5-Naming a vector (2)

```r
#Use days_vector to set the names of poker_vector and roulette_vector.
    
# Poker winnings from Monday to Friday
  poker_vector <- c(140, -50, 20, -120, 240)
  
# Roulette winnings from Monday to Friday
  roulette_vector <- c(-24, -50, 100, -350, 10)
  
# The variable days_vector
  days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
  
# Assign the names of the day to roulette_vector and poker_vector
  names(poker_vector) <-days_vector 
  names(roulette_vector) <-days_vector
```

&#x20;\#6-Calculating total winnings

```r
#Take the sum of the variables A_vector and B_vector and 
# it assign to total_vector.
#Inspect the result by printing out total_vector.    
    
A_vector <- c(1, 2, 3)
B_vector <- c(4, 5, 6)
  
# Take the sum of A_vector and B_vector
total_vector <- A_vector+B_vector
  
# Print out total_vector
total_vector
```

&#x20;\#7-Calculating total winnings (2)

```r
#Assign to the variable total_daily how much you won or lost 
#on each day in total (poker and roulette combined). 
​
# Poker and roulette winnings from Monday to Friday:
poker_vector <- c(140, -50, 20, -120, 240)
roulette_vector <- c(-24, -50, 100, -350, 10)
days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
names(poker_vector) <- days_vector
names(roulette_vector) <- days_vector
  
# Assign to total_daily how much you won/lost on each day
total_daily <- roulette_vector+ poker_vector
total_daily
```

&#x20;\#8-Calculating total winnings (3)

```r
#Calculate the total amount of money that you have won/lost-
#with roulette and assign to the variable total_roulette.
#Now that you have the totals for roulette and poker,- 
#you can easily calculate total_week-
#(which is the sum of all gains and losses of the week).
#Print out total_week.  
​
# Poker and roulette winnings from Monday to Friday:
  poker_vector <- c(140, -50, 20, -120, 240)
  roulette_vector <- c(-24, -50, 100, -350, 10)
  days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
  names(poker_vector) <- days_vector
  names(roulette_vector) <- days_vector
  
# Total winnings with poker
  total_poker <- sum(poker_vector)
  
# Total winnings with roulette
  total_roulette <- sum(roulette_vector) 
  
# Total winnings overall
  total_week <- total_roulette+total_poker
  
# Print out total_week
  total_week
```

&#x20;\#9-Comparing total winnings

```r
#Calculate total_poker and total_roulette as in the previous exercise. 
#Use the sum() function twice.
#Check if total gains in poker are higher than for roulette by 
#(using a comparison. 
#Simply print out the result of this comparison. 
#What do you conclude, should you focus on roulette or on poker?
​
# Poker and roulette winnings from Monday to Friday:
  poker_vector <- c(140, -50, 20, -120, 240)
  roulette_vector <- c(-24, -50, 100, -350, 10)
  days_vector <- c("Monday", "Tuesday", "Wednesday", 
  "Thursday", "Friday")
  names(poker_vector) <- days_vector
  names(roulette_vector) <- days_vector
  
# Calculate total gains for poker and roulette
  total_poker <-sum(poker_vector)
  total_poker
  total_roulette <-sum(roulette_vector)
  total_roulette
  
# Check if you realized higher total gains in poker than in roulette 
  total_poker>total_roulette
```

&#x20;\#10-Vector selection: the good times

```r
#Assign the poker results of Wednesday to the variable poker_wednesday.
# Poker and roulette winnings from Monday to Friday:
  poker_vector <- c(140, -50, 20, -120, 240)
  roulette_vector <- c(-24, -50, 100, -350, 10)
  days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
  names(poker_vector) <- days_vector
  names(roulette_vector) <- days_vector
  
# Define a new variable based on a selection
  poker_wednesday <- poker_vector[3]
  poker_wednesday
```

&#x20;\#11-Vector selection: the good times (2)

```r
#Assign the poker results of Tuesday, Wednesday and Thursday 
#to the variable poker_midweek.   
    
# Poker and roulette winnings from Monday to Friday:
  poker_vector <- c(140, -50, 20, -120, 240)
  roulette_vector <- c(-24, -50, 100, -350, 10)
  days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
  names(poker_vector) <- days_vector
  names(roulette_vector) <- days_vector
  
# Define a new variable based on a selection
  poker_midweek <- poker_vector[c(2,3,4)]
  poker_midweek
```

&#x20;\#12-Vector selection: the good times (3)

```r
#Assign to roulette_selection_vector the roulette results 
#from Tuesday up to Friday; 
#make use of : if it makes things easier for you. 
   
# Poker and roulette winnings from Monday to Friday:
  poker_vector <- c(140, -50, 20, -120, 240)
  roulette_vector <- c(-24, -50, 100, -350, 10)
  days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
  names(poker_vector) <- days_vector
  names(roulette_vector) <- days_vector
  
# Define a new variable based on a selection
  roulette_selection_vector <- roulette_vector[2:5]
  roulette_selection_vector
  
```

&#x20;\#13-Vector selection: the good times (4)

```r
#Select the first three elements in poker_vector by using their names:- 
#"Monday", "Tuesday" and "Wednesday". 
#Assign the result of the selection to poker_start.
#Calculate the average of the values in poker_start with 
#the mean() function. 
#Simply print out the result so you can inspect it.
​
# Poker and roulette winnings from Monday to Friday:
  poker_vector <- c(140, -50, 20, -120, 240)
  roulette_vector <- c(-24, -50, 100, -350, 10)
  days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
  names(poker_vector) <- days_vector
  names(roulette_vector) <- days_vector
  
# Select poker results for Monday, Tuesday and Wednesday
  poker_start <- poker_vector[c("Monday", "Tuesday", "Wednesday")]
  poker_start
  
# Calculate the average of the elements in poker_start
  mean(poker_start)
```

&#x20;\#14-Selection by comparison - Step 1

```r
#Check which elements in poker_vector are positive (i.e. > 0)- 
#and assign this to selection_vector.
#Print out selection_vector so you can inspect it. 
#The printout tells you whether you won (TRUE) or lost (FALSE)-
#any money for each day.    
​
​
# Poker and roulette winnings from Monday to Friday:
  poker_vector <- c(140, -50, 20, -120, 240)
  roulette_vector <- c(-24, -50, 100, -350, 10)
  days_vector <- c("Monday", "Tuesday", "Wednesday", 
  "Thursday", "Friday")
  names(poker_vector) <- days_vector
  names(roulette_vector) <- days_vector
  
# Which days did you make money on poker?
  selection_vector <- poker_vector>0
  
# Print out selection_vector
  selection_vector
```

&#x20;\#15-Selection by comparison - Step 2

```r
#Use selection_vector in square brackets to assign the amounts-
#that you won on the profitable days to the variable poker_winning_days.  
​
# Poker and roulette winnings from Monday to Friday:
  poker_vector <- c(140, -50, 20, -120, 240)
  roulette_vector <- c(-24, -50, 100, -350, 10)
  days_vector <- c("Monday", "Tuesday", "Wednesday", 
  "Thursday", "Friday")
  names(poker_vector) <- days_vector
  names(roulette_vector) <- days_vector
  
# Which days did you make money on poker?
  selection_vector <- poker_vector > 0
  selection_vector
  
# Select from poker_vector these days
  poker_winning_days <- poker_vector[selection_vector]
  poker_winning_days   
```

&#x20;\#16-Advanced selection

```r
#Create the variable selection_vector, this time to see if you 
#made profit with- 
#roulette for different days.
#Assign the amounts that you made on the days that you ended 
#positively for- 
#roulette to the variable roulette_winning_days.
#This vector thus contains the positive winnings of roulette_vector.
​
# Poker and roulette winnings from Monday to Friday:
  poker_vector <- c(140, -50, 20, -120, 240)
  roulette_vector <- c(-24, -50, 100, -350, 10)
  days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
  names(poker_vector) <- days_vector
  names(roulette_vector) <- days_vector
  
# Which days did you make money on roulette?
  selection_vector <-roulette_vector>0
  selection_vector
  
# Select from roulette_vector these days
  roulette_winning_days <- roulette_vector[selection_vector]
  roulette_winning_days  
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://r-pedia.gitbook.io/cevi/r-programming-track/a-introduction-to-r/2-vectors.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
