Dave Mason continues his quest to learn R, focusing on vectors. First, he looks at vector-based mathematical operations:
Now we can determine the number of customers gained vs number of customers lost (plus/minus) for each month of the quarter by subtracting one vector from another. Each vector has the same number of elements (three), and the result is also a vector of three elements:> net_customer_gain <- new_customers - customers_lost > net_customer_gain Jan Feb Mar -15 30 3
The sum() function can be used to add up all the elements of a vector. Below, we get the total number of new customers and lost customers for the first quarter:> sum(new_customers)  270 > sum(customers_lost)  252
To extract multiple elements from a vector, pass in an integer class vector to the square brackets. The values of the integer vector correspond to the elements to be extracted. Here we will extract the first, third, and fourth elements of the jersey_numbers vector:> jersey_numbers[c(1,3,4)] Pierce Rondo Allen 34 9 20
The values of the integer vector can be in any order:> jersey_numbers[c(4,1,3)] Allen Pierce Rondo 20 34 9
Vectors are a critical part of understanding R.