#-------------------------------------------------------------.# # VECTOR ARITHMETIC WITH TWO VECTORS# # RECYCLING RULE - used in vector arithmetic with TWO vectors when vecors are different lengths##-------------------------------------------------------------.# when you perform arithmetic between two vectors and they are the same length# then you view each correpsonding set of values as being operated on. # For examplec(100,200,300) +c(1, 2, 3) # 101 202 303
[1] 101 202 303
c(100+1 , 200+2 , 300+3) # ... same thing
[1] 101 202 303
# Another example : remember the order of operationsc(40, 20, 30) -c(4,5,6) *c(1, 2, 3) # remember the order of operations!
[1] 36 10 12
# original : c(40, 20, 30) - c(4,5,6) * c(1, 2, 3)# do the * : c(40, 20, 30) - c(4*1,5*2,6*3) # : c(40, 20, 30) - c(4,10,18)# do the - : c(40-4, 20-10, 30-18) # : c(36 , 10, 12) # if one vector is shorter than the other then ...# # step 1: In R's memory (you don't see this) the values from the # shorter vector are repeated over and over until the shorter vector# is the same length as the longer vector. ## Step 2: The math happens ...## For example the following c(10,20,30,40) +c(1,7) # same as c(10,20,30,40)+c(1,7,1,7) ... ie. 11 27 31 47
[1] 11 27 31 47
# Original: c(10,20,30,40) + c(1,7) # Recycling rule: c(10,20,30,40) + c(1,7,1,7)# addition: c(10+1,20+7,30+1,40+7)# final result: c(11, 27, 31, 47)# It doesn't matter if the shorter vector is first or lastc(1,7) +c(10,20,30,40) # same as c(1,7,1,7)+c(10,20,30,40) ... i.e. 11 27 31 47
[1] 11 27 31 47
# Doing math with a vector that contains a single value is just a special case of# this rule. Example:3*c(2,3,4) # same as c(3,3,3) * c(2,3,4)
[1] 6 9 12
# original: 3 * c(2,3,4)# recycling rule: c(3,3,3) * c(2,3,4)# multiplication: c(3*2, 3*3, 3*4)# final answer: c(6,9,12)# REMEMBER THE ORDER OF OPERATIONS!!!c(1,2) +3*c(10,20,30,40) # 31 62 91 122
# original: c(1,2) + 3 * sum(2 ^ 3 , 3-4*5)# figure out the values of the arguments: c(1,2) + 3 * sum( 8 , -17)# do the sum function: c(1,2) + 3 * -9# do the * : c(1,2) + -27# recycling rule: c(1,2) + c(-27,-27)# final answer: c(1 + -27 , 2 + -27)# final answer: c( -26 , -25)# If the length of the longer vector is not a multiple of the length of# the shorter vector, it WILL work, but you will get a WARNING. ## The warning is to alert you to the fact that you might have done something# that you didn't intend to, however, it will still work.## The recycling will continue as usual for the full length of the longer# vector.# Example:c(1,2) +c(100,200,300,400,500, 600) # 101 202 301 402 501 602
Warning in c(1, 2) + c(100, 200, 300, 400, 500): longer object length is not a
multiple of shorter object length
[1] 101 202 301 402 501
# The above command is processed as follows# original: c(1,2) + c(100,200,300,400,500) # recycling rule: c(1,2,1,2,1) + c(100,200,300,400,500)# final answer : c (101, 202, 301, 402, 501)# # Because the first vector was recycled a non-whole-number of times# R displays a warning.
6.2 ERRORs vs WARNINGs
When you get an ERROR, something is wrong and the command doesn’t have ANY effect.
By contrast, when you get a WARNING, the command DOES work. The warning is intended to bring to your attention that something might not be right. However, sometimes what might not “look right” is exactly what you intended. If so, you can just ignore the warning message.
6.2.1 Example of an ERROR - variable doesn’t exist
You can’t use a variable that doesn’t exist yet. That will generate an error message - see the example below.
# When you have an error, the command ends at the time the error happens.# Therefore, in the following command no value is assigned to the variable.## NOTE - just in case you have a variable, x, that was already created, we# will "remove" that variable on the next line. This helps us to prove our point.# this is here just to prove a point. rm(x) # remove x (just in case it already exists) to help us prove our point
Warning in rm(x): object 'x' not found
x +5# ERROR - x doesn't exist
Error: object 'x' not found
6.2.2 Another example of an ERROR - function doesn’t exist
Similarly, you can’t call a function that doesn’t exist yet. That will generate an error message - see the example below.
x =combine_stuff(10,20,30) +c(1,2,3) # ERROR - the function combine_stuff doesn't exist
Error in combine_stuff(10, 20, 30): could not find function "combine_stuff"
x # ERROR - x still doesn't exist, i.e. the above command did NOTHING
Error: object 'x' not found
6.2.3 Example of a WARNING - recyling rule issues
When you get a WARNING, the command DOES have an effect. For example, even though the following command generates a warning the assignment to the variable DOES take effect. The warning is just to attract your attention to the fact that the lenght of the longer vector is not an even multiple of the length
If you assign the result of something that produces a warning the assignment will happen and you can use that value without getting anymore warnings.
# Based on the recycling rule the following command# # nums <- c(1,2) + c(100,200,300,400,500)# # produces the same results as as the following command## nums <- c(1,2,1,2,1) + c(100,200,300,400,500)## This is because when the two vectors are not the same length, the shorter# vector gets "recycled" - i.e. the values in the shorter vector are# treated as if they are repeated until the shorter vector is the same # length as the longer vector. However, a WARNING is generated # when some values in the vector are "recycled" more times than other values.# This might be because of an oversight or it might be w# The following generates a WARNING because the recycling rule winds up # using the "1" in the first vector 3 times but the "2" in the first # vector only 2 times. nums <-c(1,2) +c(100,200,300,400,500) # 101 202 301 402 501 (with a warning)
Warning in c(1, 2) + c(100, 200, 300, 400, 500): longer object length is not a
multiple of shorter object length
nums
[1] 101 202 301 402 501
# The value of nums was still assigned and can be used normallynums
[1] 101 202 301 402 501
nums -50
[1] 51 152 251 352 451
6.3 — PRACTICE —
#----------------------------------------------------------------------------.# QUESTION## Use the recycling rule to generate the first ten multiples of 5 in a single# command.# The result should be as shown below. ## > YOUR COMMAND GOES HERE # replace this line with your command## [1] 5 10 15 20 25 30 35 40 45 50#----------------------------------------------------------------------------.#----------------------------------------------------------------------------.# QUESTION# # numValues is a variable that contains a number.# Write code that produces numValues of the 5 times table. ## EXAMPLE 1# > numValues = 3# > YOUR CODE GOES HERE# [1] 5 10 15## EXAMPLE 2# > numValues = 7# > YOUR CODE GOES HERE# [1] 5 10 15 20 25 30 35#----------------------------------------------------------------------------.# THINK ABOUT THIS - to answer the question you need ONE command that will # use the numValues variable to create the following results 5*c(1,2,3) # when numValues is 3
[1] 5 10 15
5*c(1,2,3,4,5,6,7) # when numValues is 7
[1] 5 10 15 20 25 30 35
#----------------------------------------------------------------------------.# QUESTION ###### Use the recycling rule to generate the first five multiples # of 2 and 100 using a single command. The result should be as shown below. ## > YOUR COMMAND GOES HERE # replace this line with your command## [1] 2 100 4 200 6 300 8 400 10 500#----------------------------------------------------------------------------.
#----------------------------------------------------------------------------.# QUESTION ###### Use the recycling rule to generate the first five multiples # of 2 and 100 using a single command. The result should be as shown below. # Write the command using the least amount of typing.## > YOUR COMMAND GOES HERE # replace this line with your command# [1] 2 4 6 8 10 100 200 300 400 500## The following is NOT the answer but helps you to think about how to generate# the same thing using shorter code.# c( 2 * 1 , 2 * 2 , 2*3 , 2*4 , 2*5 , 100 * 1 , 100 * 2 , 100 * 3 , 100 * 4, 100*5) #----------------------------------------------------------------------------.