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>
4Let'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.
4john_earnings
ivan_earnings
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)
3Now that we have the data, we can start working with it to measure performance.
We will use this earnings data to understand how to perform some important arithmetic operations on vectors.
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.
4total_earnings_per_day
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
4As you can see, earnings for each day are added and assigned to the new variable.
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.
4john_total
ivan_total
total_earnings
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[1By 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.
4john_vs_ivan
overall_perfromance
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 performanceJust 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.
4difference
The results will be as follows:
1> #Print the difference
2> difference
3[1] 150 20 -55 120 60
4We 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)
3We 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.
4john_earnings
ivan_earnings
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 Monday Tuesday Wednesday Thursday Friday
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.
4positive_vector
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
Monday Wednesday Friday
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.