How to Use Arithmetic Vector Operations in R
One set of arithmetic functions in R consists of functions in which the outcome is dependent on more than one value in the vector. Summing a vector with the sum() function is such an operation. Here are some others:
|Function||What It Does|
|sum(x)||Calculates the sum of all values in x|
|prod(x)||Calculates the product of all values in x|
|min(x)||Gives the minimum of all values in x|
|max(x)||Gives the maximum of all values in x|
|cumsum(x)||Gives the cumulative sum of all values in x|
|cumprod(x)||Gives the cumulative product of all values in x|
|cummin(x)||Gives the minimum for all values in x from the start of the vector until the position of that value|
|cummax(x)||Gives the maximum for all values in x from the start of the vector until the position of that value|
|diff(x)||Gives for every value the difference between that value and the next value in the vector|
How to summarize a vector in R
You can tell quite a few things about a set of values with one number. To illustrate, let’s assume you have two vectors containing the number of baskets that Granny and her friend Geraldine scored in the six games of this basketball season:
> baskets.of.Granny <- c(12,4,4,6,9,3) > baskets.of.Geraldine <- c(5,3,2,2,12,9)
If you want to know the minimum and maximum number of baskets Granny made, for example, you use the functions min() and max():
> min(baskets.of.Granny)  3 > max(baskets.of.Granny)  12
To calculate the sum and the product of all values in the vector, use the functions sum() and prod(), respectively.
These functions also can take a list of vectors as an argument. If you want to calculate the sum of all the baskets made by Granny and Geraldine, you can use the following code:
> sum(baskets.of.Granny,baskets.of.Geraldine)  75
Missing values always return NA as a result. The same is true for vector operations as well. R, however, gives you a way to simply discard the missing values by setting the argument na.rm to TRUE. Take a look at the following example:
> x <- c(3,6,2,NA,1) > sum(x)  NA > sum(x,na.rm=TRUE)  12
This argument works in sum(), prod(), min(), and max().
How to cumulate operations in R
Suppose that after every game, you want to update the total number of baskets that Granny made during the season. After the second game, that’s the total of the first two games; after the third game, it’s the total of the first three games; and so on. You can make this calculation easily by using the cumulative sum function, cumsum(), as in the following example:
> cumsum(baskets.of.Granny)  12 16 21 27 36 39
In a similar way, cumprod() gives you the cumulative product. You also can get the cumulative minimum and maximum with the related functions cummin() and cummax().
How to calculate differences in R
You can calculate the difference in the number of baskets between every two games Granny played by using the following code:
> diff(baskets.of.Granny)  -8 1 1 3 -6
You get five numbers back. The first one is the difference between the first and the second game, the second is the difference between the second and the third game, and so on.
The vector returned by diff() is always one element shorter than the original vector you gave as an argument.