Introduction to Vectors in R. Learn how to analyze your gambling... | by Linda Ngo | Towards Data Science

Let’s take a trip to a Statisticians Paradise, otherwise known as Vegas!
Thanks to R and your new data analytical skills, you will learn how to uplift your performance at the tables and make some profits. After this tutorial, you will be able to easily track your betting progress and analyze your past action.
Vectors are one-dimensional arrays that can store numeric data, character data, or logical data. They are a simple tool to store data. For example, you can store your daily gains and losses in the casinos.
In R, you create a vector with the combine function c() . You place the vector elements separated by a comma between the parentheses. For example:
numeric_vector <- c(1, 2, 3)character_vector <- c("a", "b", "c")
Once you have created these vectors in R, you can use them to do calculations.
Create a vector that contains the three elements: TRUE , FALSE , and TRUE (in that order)
boolean_vector <- c(TRUE, FALSE, TRUE)
After a week in Vegas, you still aren’t rolling in dough. So, you decide to start using your new data analysis skills.
Before doing an analysis first, you decide to collect the outcomes for the last week:
For Poker:
On Monday you won $140.
Tuesday you lost $50.
Wednesday you won $20.
Thursday you lost $120.
Friday you won $240.
For Roulette:
On Monday you lost $24.
Tuesday you lost $50.
Wednesday you won $100.
Thursday you lost $350.
Friday you won $10.
You only played poker and roulette. Assign the winnings/losses to variables.
# Poker winnings from Monday to Fridaypoker_vector <- c(140, -50, 20, -120, 240)# Roulette winnings from Monday to Fridayroulette_vector <- c(-24, -50, 100, -350, 10)
As a data analyst, it is important to have a clear view on the data being used. Thus, understanding what each element refers to is essential.
In the previous exercise, we created a vector with your winnings over the week. Each vector element refers to a day of the week, but it is hard to tell which element belongs to which day.
To show that in the vector itself by giving a name to the elements of a vector with the names() function. Here’s an example:
some_vector <- c("John Doe", "poker player"
This code first creates a vector some_vector and then gives the two elements a name. The first element is assigned the name Name , while the second element is labeled Profession . Printing the contents to the console yields the following output:
Name the elements of your poker and roulette vector from the previous exercise with the days of the week.
# Poker winnings from Monday to Fridaypoker_vector <- c(140, -50, 20, -120, 240)# Roulette winnings from Monday to Fridayroulette_vector <- c(-24, -50, 100, -350, 10)# Assign days as names of poker_vectornames(poker_vector) <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")# Assign days as names of roulette_vectornames(roulette_vector) <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
In the previous exercise, it may have been boring and frustrating to type and retype information such as the days of the week. There is a more efficient way to do this, namely, to assign the days of the week vector to a variable.
Just like you did with the poker and roulette returns, you can also create a variable that contains the days of the week. This way, you can use and re-use it.
Create a vector that contains the days of the week. Use this vector to set the names of your poker and roulette vectors. You will get the same output as you did from the previous exercise.
# Poker winnings from Monday to Fridaypoker_vector <- c(140, -50, 20, -120, 240)# Roulette winnings from Monday to Fridayroulette_vector <- c(-24, -50, 100, -350, 10)# The variable days_vectordays_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")# Assign the names of the day to roulette_vector and poker_vectornames(poker_vector) <- days_vectornames(roulette_vector) <- days_vector
Now that you have the poker and roulette returns names, you can starting some data analysis.
You want to find out the following type of information:
How much has been your overall profit or loss per day of the week?
Have you lost money over the week in total?
Are you winning/losing money on poker or on roulette?
To get the answers, you have to do arithmetic calculations on vectors.
If you sum two vectors in R, it takes the element-wise sum. For example, the following three statements are completely equivalent:
c(1, 2, 3) + c(4, 5, 6)c(1 + 4, 2 + 5, 3 + 6)c(5, 7, 9)
You can also do the calculations with variables that represent vectors:
a <- c(1, 2, 3) b <- c(4, 5, 6)c <- a + b
The overall daily profit is the sum of the profit/loss realized on poker per day, and the profit/loss realized on roulette per day.
Assign to a variable how much you won/lost on each day in total (poker and roulette combined).
# Poker winnings from Monday to Fridaypoker_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_vectornames(roulette_vector) <- days_vector# Assign to total_daily how much you won/lost on each daytotal_daily <- poker_vector + roulette_vector# Print the total won/lost per daytotal_daily
Based on the previous analysis, it looks like you had a mix of good and bad days. Is there a change you may have lost money over the week in total?
To answer this question, use the function sum() . It calculates the sum of all elements of a vector. For example, to calculate the total amount of money you have lost/won with poker:
total_poker <- sum(poker_vector)
Calculate the total amount of money you have won/lost with roulette.
After you have the totals for roulette and poker, calculate the sum of all gains/losses of the week.
# Poker winnings from Monday to Fridaypoker_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_vectornames(roulette_vector) <- days_vector# Total winnings with pokertotal_poker <- sum(poker_vector)# Total winnings with roulettetotal_roulette <- sum(roulette_vector)# Total winnings overalltotal_week <- total_poker + total_roulette# Print out total_weektotal_week
It looks like you are losing money. You’ll need some deeper analysis to adapt your strategy.
Is it possible that you are better in one game than the other? Maybe your total gains in poker are higher (or > ) than in roulette.
Calculate the total gains in poker and roulette as in the previous exercise. Check if your total gains in poker are higher than for roulette by using a comparison. Print out the result of this comparison.
# Poker winnings from Monday to Fridaypoker_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_vectornames(roulette_vector) <- days_vector# Calculate total gains for poker and roulettetotal_poker <- sum(poker_vector)# Check if you realized higher total gains in poker than in roulettetotal_poker > total_roulette
It appears that you are better at poker than roulette.
Let’s investigate your performance at the beginning of the working week compared to the end of it. You did have a couple of “vacation” drinks at the end of the week...
To investigate this, you will only focus on a selection of the total_vector . In other words, our goal is to select specific elements of the vector. To select elements of the vector, you can use square brackets (the same is true for matrices, data frames, ...). Between the square brackets, indicate what element to select. For example, to select the first element of the vector, you use poker_vector[1] . To select the second element, use poker_vector[2] , and so on for the remaining elements. Notice that the first element has index 1, not 0 (as in many other programming languages).
Let’s analyze your midweek results.
To select multiple elements from a vector, indicate which elements should be selected using a vector within the square brackets. For example, to select the first and fifth day of the week, use the vector c(1,5) between the square brackets. poker_vector[c(1,5)] will give the first and fifth element of poker_vector .
Another way to do this is the define the selection vector as a range. That is, c(2,3,4) can be abbreviated to 2:4 . We can then use poker_vector[2:4] to find the mid-week results.
You can also select elements using the names of the vector elements (e.g. Monday, Tuesday,...) instead of their numeric positions. For example, poker_vector["Monday"] will select the element with the name "Monday" . This is the first element of poker_vector since "Monday" is the name of the first element.
Assign the poker results of Wednesday to a variable.
# Poker winnings from Monday to Fridaypoker_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_vectornames(roulette_vector) <- days_vector# Defined a new variable based on a selectionpoker_wednesday <- poker_vector[3]
Assign the poker results of Tuesday, Wednesday, and Thursday to a variable.
# Poker winnings from Monday to Fridaypoker_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_vectornames(roulette_vector) <- days_vector# Define a new variable based on a selection (Method 1)poker_midweek <- poker_vector[c(2,3,4)]# Method 2 - abbreviating the vector rangepoker_midweek <- poker_vector[2:4]# Method 3 - using the names of the elementspoker_midweek <- poker_vector[c("Tuesday", "Wednesday", "Thursday")]
The (logical) comparison operators known to R are:
< for less than
> for greater than
<= for less than or equal to
>= for greater than or equal to
== for equal to each other
!= for not equal to each other
We can use these comparison operators on vectors. For example,
> c(4, 5, 6) > 5[1] FALSE FALSE TRUE
This command tests for every element of the vector if the condition stated by the comparison operators is TRUE or FALSE . In the example, this checks if each element of the vector is greater than 5.
Check which elements in the poker vector are positive (i.e. >0) .
# Poker winnings from Monday to Fridaypoker_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_vectornames(roulette_vector) <- days_vector# Which days did you make money on poker?selection_vector <- poker_vector > 0# Print out selection_vectorselection_vector
Working with comparisons will make your data analysis easier. Instead of selecting a subset of days to investigate yourself (like before, where you selected between Tuesday, Wednesday, and Thursday), you can simply ask R to return only those days where you realized a positive return for poker.
Using selection_vector <- poker_vector > 0 , you can find the days on which you had a positive poker return. Suppose you would like to know not only the days on which you won, but also how much you won on those days.
You can select the desired elements, by putting selection_vector between the square brackets that follow poker_vector :
poker_vector[selection_vector]
R knows what to do when you pass a logical vector in square brackets: it will only select the elements that correspond to TRUE in selection_vector .
Assign the amounts that you won on the profitable poker days to a variable.
# Poker winnings from Monday to Fridaypoker_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_vectornames(roulette_vector) <- days_vector# Which days did you make money on poker?selection_vector <- poker_vector > 0# Select from poker_vector these dayspoker_winning_days <- poker_vector[selection_vector]
Just like you did for poker, determine which days you realized a positive return for roulette. Create a variable that contains the positive winnings from roulette.
# Poker winnings from Monday to Fridaypoker_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_vectornames(roulette_vector) <- days_vector# Which days did you make money on roulette?selection_vector <- roulette_vector > 0# Select from poker_vector these daysroulette_winning_days <- roulette_vector[selection_vector]
All images, unless specified, are owned by the author. The banner image was created using Canva.