Creating and Using Vectors in R
In R, a vector is a series of data elements of the same data type, for example, a series of numbers, a series of characters, or a series of logical values. In finance, for example, the daily profit/loss from your stock investments could be represented as a vector. A vector is also referred to as a one-dimensional array.
An important point to remember is that within a vector, all the data elements must be of the same type, i.e., you cannot mix different data types in the same vector.
In R, you can create a vector using the combine()
function. For example, here is a vector containing three numeric values 5
, 7
and 8
.
1> c(5,7,8)
2[1] 5 7 8
3>
4
Let's say John and Ivan, two colleagues engage in day trading for a week and want to analyze their daily performance. Their daily profit and loss from the trading activity is shown below:
We can use this data in R by creating vectors and assigning them to variables.
1#John's earnings in the week
2john_earnings <- c(120,-40,25,-130,260)
3#Ivan's earnings in the week.
4ivan_earnings <- c(-30,-60,80,-250,200)
5#Print John's earnings
6john_earnings
7#Print Ivan's earnings
8ivan_earnings
9
Run this script in RStudio (CTRL+ENTER) and you will see the results in RConsole.
1> #John's earnings in the week
2 > john_earnings <- c(120,-40,25,-130,260)
3 >
4 > #Ivan's earnings in the week.
5 > ivan_earnings <- c(-30,-60,80,-250,200)
6 >
7 > #Print John's earnings
8 > john_earnings
9 [1] 120 -40 25 -130 260
10 >
11 > #Print Ivan's earnings
12 > ivan_earnings
13 [1] 120 -40 25 -130 260
14 >
15
Now that we have the data, we can start working with it to measure performance.
Vector Operations
We will use this earnings data to understand how to perform some important arithmetic operations on vectors.
Combined Earnings Per Day
We can calculate the combined earnings of both John and Ivan by just adding the two vectors. When you add two vectors, the addition is performed member-wise (i.e., each element in the vector is added to the element on the same index in the other vector.
1#John's earnings in the week
2john_earnings <- c(120,-40,25,-130,260)
3#Ivan's earnings in the week.
4ivan_earnings <- c(-30,-60,80,-250,200)
5#Calculate total earnings
6total_earnings_per_day = john_earnings + ivan_earnings
7#Print total earnings
8total_earnings_per_day
9
Run this script in your computer and compare the results with the ones below:
1> #Print Ivan's earnings
2> total_earnings_per_day
3[1] 90 -100 105 -380 460
4
As you can see, earnings for each day are added and assigned to the new variable.
Total Earnings
The next thing we want to know is - "What were their total earnings for the whole week?"
We can claculate thus using the Sum()
function in R which calculates the sum of all elements of a vector.
1#John's earnings in the week
2john_earnings <- c(120,-40,25,-130,260)
3#Ivan's earnings in the week.
4ivan_earnings <- c(-30,-60,80,-250,200)
5#Calculate John's total earnings for the week
6john_total <- sum(john_earnings)
7#Calculate Ivan's total earnings for the week
8ivan_total <- sum(ivan_earnings)
9# calculate total combined earnings for the week
10total_earnings <- john_total + ivan_total
11#Print individual totals and combined total
12john_total
13ivan_total
14total_earnings
15
Run this script in your computer and compare the results with the ones below:
1> #Print individual totals and combined total
2> john_total
3[1] 235
4> ivan_total
5[1] -60
6> total_earnings
7[1] 175
8>
9
Compare Performance
By visually analyzing the above numbers we already know that John has performed much better than Ivan. However, when we deal with large datasets, such visual scans will not be possible. So, we can give the job of comparing performance to R script.
In the following script, we compare performance as a daily level as well as total. To compare the daily performance, we just have to compare the two vectors. For example, we can check "Did John perform better than Ivan each day?". To do so, we can use the logical operator to compare the two vectors. R will automatically compare each element in the vector and return a boolen value.
1#John's earnings in the week
2john_earnings <- c(120,-40,25,-130,260)
3#Ivan's earnings in the week.
4ivan_earnings <- c(-30,-60,80,-250,200)
5#Check if John performed better than Ivan each day
6john_vs_ivan <- john_earnings > ivan_earnings
7#Calculate John's total earnings for the week
8john_total <- sum(john_earnings)
9#Calculate Ivan's total earnings for the week
10ivan_total <- sum(ivan_earnings)
11#Check if John performed better than Ivan overall
12overall_perfromance <- john_total > ivan_total
13#Print John's performance vis-a-vis Ivan's each day
14john_vs_ivan
15#Print overall performance
16overall_perfromance
17
The results should match our visual understanding of the numbers:
1> #Print John's performance vis-a-vis Ivan's each day
2> john_vs_ivan
3[1] TRUE TRUE FALSE TRUE TRUE
4>
5> #Print overall performance
6> overall_perfromance
7[1] TRUE
8>
9
Other Arithmetic Operations
Just like addition we can perform all arithmetic operations on vectors such as subtraction, multiplication, and division. The following example shows the difference between John's and Ivan's earnings each day.
1#John's earnings in the week
2john_earnings <- c(120,-40,25,-130,260)
3#Ivan's earnings in the week.
4ivan_earnings <- c(-30,-60,80,-250,200)
5#Calculate difference in earnings
6difference <- john_earnings - ivan_earnings
7#Print the difference
8difference
9
The results will be as follows:
1> #Print the difference
2> difference
3[1] 150 20 -55 120 60
4>
5
Vector Index
We can retrieve individual values from a vector by referring to its index. For example, you can select the first element in the vector by typing vector_name[1]
. Similarly, you can select multiple values from the vector by using the notations demonstrated below:
1> #John's earnings in the week
2> john_earnings <- c(120,-40,25,-130,260)
3>
4> #Ivan's earnings in the week.
5> ivan_earnings <- c(-30,-60,80,-250,200)
6>
7> #Select John's earnings on 3rd day
8> john_earnings[3]
9[1] 25
10>
11> #Select John's earnings on 2nd and 5th day
12> #You can select multiple values by supplying a vector of indexes
13>
14> john_earnings[c(2,5)]
15[1] -40 260
16>
17> #Select John's earnings from 2nd to 4th day
18> #To produce a vector slice between two indexes,
19> #you can use the colon operator ":". Helpful for large vectors.
20>
21> john_earnings[2:4]
22[1] -40 25 -130
23>
24
Naming Vector Members
We learned how to refer to the elements of the vector but it's still difficult to tell which earnings belong to which day. In such situations, we can assign names to the members of the vectors. In our case, we can assign the names of days to the elements. We can do so by using the names()
function as shown below:
1#John's earnings in the week
2john_earnings <- c(120,-40,25,-130,260)
3#Ivan's earnings in the week.
4ivan_earnings <- c(-30,-60,80,-250,200)
5#Assign the names of the day to John's Earnings vector
6names(john_earnings) <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
7#If you're applying these names to multiple vectors, you can even
8#create a new vector with these names and then assign it.
9days_in_week <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
10#Assign the names of the day to Ivan's Earnings vector
11names(ivan_earnings) <- days_in_week
12#Print John's and Ivan's earnings
13john_earnings
14ivan_earnings
15
Results will now look like below:
1> #Print John's and Ivan's earnings
2 > john_earnings
3 Monday Tuesday Wednesday Thursday Friday
4 120 -40 25 -130 260
5 > ivan_earnings
6 Monday Tuesday Wednesday Thursday Friday
7 -30 -60 80 -250 200
8 >
9
Select Only Positive Earnings
Let's learn a few more things about vectors using our earnings example. Let's say we want to create a new variable, which contains only the positive earnings. In this case we will use the combined earnings vector for John and Ivan.
Step 1: Create a logical vector that tells us on which days we have positive earnings
1#John's earnings in the week
2john_earnings <- c(120,-40,25,-130,260)
3#Ivan's earnings in the week.
4ivan_earnings <- c(-30,-60,80,-250,200)
5#Total Earnings
6total_earnings <- john_earnings + ivan_earnings
7#Vector for days in week
8days_in_week <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
9#Assign the names of the day to Total Earnings vector
10names(total_earnings) <- days_in_week
11#On which days did we have positive earnings
12#To do so we use comparison operators on vectors. The comparison operator
13#will compare each element in the vector if the condition stated by the
14#comparison operator is TRUE or FALSE
15positive_vector <- total_earnings > 0
16#Print the positive_vector
17positive_vector
18
The positive vector should look like this:
Step 2 is to use the positive selection vector to slice the positive earnings from total_earnings
vector.
1> #Select the positive earing days from the total_earnings vector
2
3> positive_earning_days <- total_earnings[positive_vector]
4
5>
6
7> #Print the positive earnings days
8
9> positive_earning_days
10
11Monday Wednesday Friday
12
1390 105 460
14
15>
16
In the second step, we used the logical index vector to slice our earnings factor. If the logical value is TRUE, the member is included in the resulting slice, otherwise not.
Create Your Free Account
Create a free account to access this content and join our community of learners.
You'll get access to:
- Access the full tutorial
- Join our learning community
- Track your progress
- Bookmark content for later