36  Practice Questions - TOPIC: Vectors1

##################################################################
##################################################################
## INSTRUCTIONS
## 
## Answer the questions below by writing R commands.
## There are hints after many of the questions. You can follow those
## hints. However, there are many different ways to write code to 
## accomplish something. Feel free to experiment with different ways
## to accomplish the same thing.
##
## - In general, each question should be completed using a SINGLE
##   command (unless specified otherwise).
##
## - If a question asks you to create a variable - do 
##   that in one command.
##
## - If another part of the same question then asks you to use that
##   variable - do that in another command. 
##
## - In general each question is independent of other questions.
##   However, a multi-part question (e.g. (a), (b), etc) may refer to the
##   earlier steps
##
## - For the purpose of grading all steps in each multi-step question
##   i.e. (a), (b), etc. counts as a full question
##   (i.e. each part of every question is worth the same amount of points).
##################################################################
##################################################################
# Question 1 (a).
#
#     Create a variable named "nums" that contains numbers starting with 100.
#     Each successive number should be 7 less than the previous number. Follow
#     this pattern through the negative numbers. Do not go past -100.
#     Remember to store the vector in the variable named "nums".
#     (i.e. nums should contain the vector
#          100 93 86 79 ... 9 2 -5 -12 ... -82  -89 -96 )
#
#     Hints
#       - use the seq function with the from, to and by arguments
#############.
# ANSWER 
#############.

# since we're counting DOWN, the by value must be negative
nums = seq(100,-100,by=-7)  

# Here are the numnbers
nums
 [1] 100  93  86  79  72  65  58  51  44  37  30  23  16   9   2  -5 -12 -19 -26
[20] -33 -40 -47 -54 -61 -68 -75 -82 -89 -96
# Question 1 (b).
#     Create a vector starting with the pattern 1 2 3 etc ...
#     The vector should be as long as the nums vector from
#     part (a). 
#
#     Hints:
#       - use the seq function or the colon operator (your choice)
#       - use the length function as part of your answer
#       - use the length function to check the length of nums from 
#         part (a) to make sure that your answer is correct
#############.
# ANSWER 
#############.

# one way - with seq function
seq(1, length(nums))
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29
# another way - with : operator
1:length(nums)
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29
# Question 1 (c).
#     Do part (b) again, but this time generate the numbers
#     2 4 6 etc ... The vector should be as long as the nums 
#     vector from part (a).
#     The answer should display the numbers: 2 4 6 ... 56 58
#
#     Hints: 
#       - use the seq function with the from, by and length.out arguments
#############.
# ANSWER 
#############.
# Note that you don't have to type 
# from= since from is the first argument as defined in the help info (i.e. ?seq) 
# and the value 2 is typed in the 1st position of the function call. 
# However, it is required to specify by= and length.out=
# since these arguments as specified in the function call below 
# are not in the same positions as defined in the help info.
seq(from=2, by=2, length.out=length(nums))
 [1]  2  4  6  8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
[26] 52 54 56 58
# As noted in the comment above, the following is the same as the previous line
seq(2, by=2, length.out=length(nums))
 [1]  2  4  6  8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
[26] 52 54 56 58
# Question 1 (d).
#     Subtract the vector in part (c) from the "nums" vector you created
#     in part (a). Since the answer to part (a) should contain 
#       100 93 86 ... -89 -96
#     and the answer to part (c) should contain the values
#       2   4  6  ...  56  58
#     the answer for this question should display the values
#       98  89 80 ... -145 -154
#############.
# ANSWER 
#############.
nums - seq(from=2,by=2, length.out=length(nums))
 [1]   98   89   80   71   62   53   44   35   26   17    8   -1  -10  -19  -28
[16]  -37  -46  -55  -64  -73  -82  -91 -100 -109 -118 -127 -136 -145 -154
# Question 1 (e).
#     Do part (c) again, but this time do NOT use the length
#     function. Instead use the along.with
#     argument to the seq function. We did NOT cover along.with
#     in class, but you should get used to learning how to 
#     learn about R functions. See the documentation for the seq
#     function or search online for more information.
# along.with 
seq(2, along.with=nums, by=2)
 [1]  2  4  6  8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
[26] 52 54 56 58
# Question 2.
#    Create a vector with the pattern 5 10 15 ... 50 repeated
#    four times (i.e. 5 10 15 ... 50 5 ... 50 5 ... 50 5 ... 50)
#    Hints:
#      - create the vector 5 10 15 ... 50 using the seq function
#        by specifying the from, to and by arguments
#
#      - modify your answer by using the rep function to repeat that
#        vector four times
#############.
# ANSWER 
#############.
rep(seq(from=5,to=50,by=5), times=4)
 [1]  5 10 15 20 25 30 35 40 45 50  5 10 15 20 25 30 35 40 45 50  5 10 15 20 25
[26] 30 35 40 45 50  5 10 15 20 25 30 35 40 45 50
# Question 3.
#
# Create a vector that contains 3 copies of each of the even numbers
# from 2 through 100. Store this in a variable named trippleEvens. 
# i.e. trippleEvens should contain the values
#         2 2 2 4 4 4 6 6 6 ... 98 98 98 100 100 100
#
# Hints:
#   - create a vector of just the even numbers (i.e. 2 4 6 ... 100)
#     by using the seq function with arguments, from, to, by
#
#   - extend your code by using the rep function with the "each" argument
#############.
# ANSWER 
#############.
rep( seq(from=2, to=100, by=2), each=3)
  [1]   2   2   2   4   4   4   6   6   6   8   8   8  10  10  10  12  12  12
 [19]  14  14  14  16  16  16  18  18  18  20  20  20  22  22  22  24  24  24
 [37]  26  26  26  28  28  28  30  30  30  32  32  32  34  34  34  36  36  36
 [55]  38  38  38  40  40  40  42  42  42  44  44  44  46  46  46  48  48  48
 [73]  50  50  50  52  52  52  54  54  54  56  56  56  58  58  58  60  60  60
 [91]  62  62  62  64  64  64  66  66  66  68  68  68  70  70  70  72  72  72
[109]  74  74  74  76  76  76  78  78  78  80  80  80  82  82  82  84  84  84
[127]  86  86  86  88  88  88  90  90  90  92  92  92  94  94  94  96  96  96
[145]  98  98  98 100 100 100
# Question 4.
#        Repeat the values 3 6 9 ... 60 as many times as 
#        a necessary until you reach a vector of length 150
#        (i.e. 3 6 9 ... 60 3 6 9 ... 60 3 ... etc until a total length of 150)
#        Store the result in the variable named "tens".
#
#        Hints: 
#          - use the seq function to create the vector 3 6 9 ... 60 
#          - expand your answer by using the rep function with the length.out
#            argument
#          - Make sure that the vector has 150 values by using the length
#            function.
#############.
# ANSWER 
#############.
rep( seq(from=3, to=60, by=3), length.out=150)
  [1]  3  6  9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60  3  6  9 12 15
 [26] 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60  3  6  9 12 15 18 21 24 27 30
 [51] 33 36 39 42 45 48 51 54 57 60  3  6  9 12 15 18 21 24 27 30 33 36 39 42 45
 [76] 48 51 54 57 60  3  6  9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60
[101]  3  6  9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60  3  6  9 12 15
[126] 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60  3  6  9 12 15 18 21 24 27 30
# Question 5 (a).
#
#    Display the powers of 10 from 10^9 down to 10^1.
#
#        Hints: 
#          - Use vector arithmetic. Raise 10 to the power of
#            a vector that contains the numbers 1 2 3 ... 9
#            You can create the vector 1 2 3 ... 9 with the 
#            colon operator.
#############.
# ANSWER 
#############.
# Note that the parentheses ARE required since ^ comes before : in the 
# order of operations ( see ?Syntax )
#
# 10^1:9 would NOT produce the correct results.
#
# Also note that since 10^9 is a rather large number, R displays the values in
# "scientific notation". (We will learn more about scientific notation later)
10^(1:9)
[1] 1e+01 1e+02 1e+03 1e+04 1e+05 1e+06 1e+07 1e+08 1e+09
# Question 5 (b).
#
#     Display the square roots of the powers of 
#     ten starting from the square root of 10^9 down to the 
#     square root of 10. 
#
#        Hints: 
#          - create the vector 9 8 7 ... 1 using the colon operator
#          - raise 10 to the power of that vector
#          - pass the whole expression to the sqrt function
#############.
# ANSWER 
#############.
sqrt(10^(9:1))
[1] 31622.776602 10000.000000  3162.277660  1000.000000   316.227766
[6]   100.000000    31.622777    10.000000     3.162278
# Question 6 (a).
#
#     Create a vector that contains 30 numbers, starting with 1 and 
#     ending with 5. The numbers should be evenly spaced.
#
#        Hints:
#          - use the seq function with the from, to and length.out arguments
#############.
# ANSWER 
#############.
seq(from=1, to=5, length.out=30)
 [1] 1.000000 1.137931 1.275862 1.413793 1.551724 1.689655 1.827586 1.965517
 [9] 2.103448 2.241379 2.379310 2.517241 2.655172 2.793103 2.931034 3.068966
[17] 3.206897 3.344828 3.482759 3.620690 3.758621 3.896552 4.034483 4.172414
[25] 4.310345 4.448276 4.586207 4.724138 4.862069 5.000000
# Question 6 (b).
#
#     Do the same as (a) but round the values to the nearest hundredth
#     place (i.e. 2nd number after decimal point)
#
#        Hints:
#          - use the round function with the digits argument
#############.
# ANSWER 
#############.
round ( seq(from=1, to=5, length.out=30), digits=2)
 [1] 1.00 1.14 1.28 1.41 1.55 1.69 1.83 1.97 2.10 2.24 2.38 2.52 2.66 2.79 2.93
[16] 3.07 3.21 3.34 3.48 3.62 3.76 3.90 4.03 4.17 4.31 4.45 4.59 4.72 4.86 5.00
# Question 7.
#    Create a vector that contains the 1st, 2nd, 3rd and 4th powers 
#    of the numbers from 1 through 10 - there should be 40 values in all.
#    (i.e.  1^1 1^2 1^3 1^4 2^1  2^2 2^3 2^4 3^1 3^2 3^3 3^4 ... 10^1 10^2 10^3 10^4 
#     which should be displayed as:
#           1   1   1   1   2    4   8   16  3   9   27  81  ... 10   100  1000 10000)
#
#    Hints:
#      - There should be 40 numbers in all. We will use vector arithmetic ...
#      - Therefore create 2 different vectors ... each will have 40 values.
#      - The first vector should contain the numbers
#           1 1 1 1 2 2 2 2 3 3 3 3 ... 10 10 10 10
#      - The 2nd vector should contain the powers 1 2 3 4 1 2 3 4 etc.
#      - Raise the first vector to the power of the 2nd vector using the ^ operator.
#############.
# ANSWER 
#############.

# One answer - this version uses the recycling rule - this is the preferred answer.
# Note that according to the order of operations (see ?Syntax) the ^ operator
# (i.e. exponentiation) has a higher precedence than the colon operator (i.e. :)
# Therefore, the parentheses around (1:4) ARE necessary.
rep(1:10, each=4) ^ (1:4)
 [1]     1     1     1     1     2     4     8    16     3     9    27    81
[13]     4    16    64   256     5    25   125   625     6    36   216  1296
[25]     7    49   343  2401     8    64   512  4096     9    81   729  6561
[37]    10   100  1000 10000
# Another answer - this version doesn't rely on the recycling rule.
# In this version the 2nd vector is explicitly repeated to a length of 40.
# As shown in the previous answer, this is actually not necessary.
rep(1:10, each=4) ^ rep(1:4, length.out=40)
 [1]     1     1     1     1     2     4     8    16     3     9    27    81
[13]     4    16    64   256     5    25   125   625     6    36   216  1296
[25]     7    49   343  2401     8    64   512  4096     9    81   729  6561
[37]    10   100  1000 10000
# Question 8.
#
# Write code that displays the 4th value from the vector nums.
# (nums could contain anything)
#############.
# ANSWER 
#############.
nums = c(10,20,30,40,50,60,70,80,90,100)   # make up some sample data
nums[4]  # Answer
[1] 40
# Question 10.
#
# Write code that displays the 4th and 6th values from the vector nums.
# (nums could contain anything)
#############.
# ANSWER 
#############.

# make up some sample data
nums = c(10,20,30,40,50,60,70,80,90,100)   

# Answer - the c() IS REQUIRED here since there must be only a single
# vector between the [brackets]
nums[c(4,6)]  
[1] 40 60
# Question 11.
#
# Write code that displays 3 copies of the 2nd value from the vector nums.
# (nums could contain anything)
# For example if nums contains 10 20 30 40
# Then your code should display 20 20 20
#
# Hints:
#   - pass the 2nd value of nums to the rep function
#############.
# ANSWER 
#############.

# make up some sample data
nums = c(10,20,30,40,50,60,70,80,90,100)   

# Answer
rep(nums[2] , 3)
[1] 20 20 20
# Question 12.
#
# Write code that displays the 3rd through the 30th values from the vector nums.
# (nums could contain anything)
#
# Hints:
#   - create a vector that contains the numbers 3 4 5 ... 30
#   - display the values in those positions from nums by 
#     placing the code that creates the vector 3 4 5 ... 30
#     between [brackets] next to the name "nums"
#############.
# ANSWER 
#############.

# make up some sample data
nums = seq(10,length.out=40,by=10)
nums
 [1]  10  20  30  40  50  60  70  80  90 100 110 120 130 140 150 160 170 180 190
[20] 200 210 220 230 240 250 260 270 280 290 300 310 320 330 340 350 360 370 380
[39] 390 400
# Answer
nums [ 3:30 ]
 [1]  30  40  50  60  70  80  90 100 110 120 130 140 150 160 170 180 190 200 210
[20] 220 230 240 250 260 270 280 290 300
# Question 13.
#
# Display the values that are in the even positions of nums. 
# nums may contain anything. For example if nums contains
# the values 10 20 30 40 50 60 70 80
# then your code should display 20 40 60 80.
#
# Hints:
#   - use the seq function to create a vector of even numbers.
#     2 4 6 ... The vector should have as many numbers in it
#     as 1/2 the length of nums. For example, if nums contains
#     10 20 30 40 50 60 70 80 then the vector of even numbers
#     should contain 2 4 6 8
#
#   - use the vector of even numbers between the [brackets]
#     used to retrive specific positions from the nums
#     vector
#############.
# ANSWER 
#############.

# Setup some sample data
nums = c(10,20,30,40,50,60,70,80)

# One answer - in two lines
evenPositions = seq(2,length(nums),2)
nums[evenPositions]   
[1] 20 40 60 80
# Another answer - all in one line
nums[seq(2,length(nums),2)]
[1] 20 40 60 80
# Question 14.
#
# Display all values from the vector nums EXCEPT for the
# 2nd and 5th values. nums can contain any values.
# For example if nums contains 10 20 30 40 50 60
# then your code should display the numbers 10 30 40 60
# Hints: 
#   - use a negative indexes between the [brackets], i.e. -2
#############.
# ANSWER 
#############.

# Setup some sample data
nums = c(10,20,30,40,50,60)

# One answer
nums[c(-2,-5)]
[1] 10 30 40 60
# Another answer, use the negation operator (i.e. minus sign) 
# to change the values to negatives
nums[- c(2,5)]
[1] 10 30 40 60
# Question 15. 
#
# Display all values from the vector nums EXCEPT for the
# 3rd, 6th, 9th, etc values.
#
# For example if nums contains 10 20 30 40 50 60 70 80 90 100
# then your code should display 1st, 2nd, 4th, 5th, 7th, 8th and 10th values
# i.e. the numbers 10 20 40 50 70 80 100
#
# Hints:
#   - write code to create a vector that contains the numbers 3 6 9 ... etc
#     The last number of this vector should not be larger than 
#     the number of values in nums.
#
#   - negate those values to get -3 -6 -9 ... etc
#
#   - use that vector between the [brackets] next to nums to get
#     all values from nums except the numbers in the specified positions.
#############.
# ANSWER 
#############.

# Setup some sample data
nums = seq(from=10, to=100, by=10)
nums 
 [1]  10  20  30  40  50  60  70  80  90 100
# One answer, use one - sign before the call to seq
nums[ - seq(3,length(nums), by=3) ]
[1]  10  20  40  50  70  80 100
# Another answer, use negative from to and by arguments to seq
nums[ seq(-3,-length(nums), by=-3) ]
[1]  10  20  40  50  70  80 100
# Question 16. 
#
# Create a vector named "rainbow" that contains the names of colors of the 
# rainbow - i.e. red orange yellow green blue indigo violet.
#############.
# ANSWER 
#############.

# One answer - with "double quotes"
rainbow = c("red","orange","yellow","green","blue","indigo","violet")
rainbow
[1] "red"    "orange" "yellow" "green"  "blue"   "indigo" "violet"
# Another answer - with 'single quotes' (i.e. apostrophes)
rainbow = c('red','orange','yellow','green','blue','indigo','violet')

# Note that even though we defined the variable with 'single quotes'
# it gets displayed by R with "double quotes" (see the output)
rainbow
[1] "red"    "orange" "yellow" "green"  "blue"   "indigo" "violet"
# Question 17.
#
# (a) Create a vector that contains the following two sentences. 
#     Store the vector in a variable named sentences. 
#     When you are done, the command :  length(sentences) 
#     should return the number 2.
#
#     Sue said "Hi" to Joe.
#     What's up with you?
#
# (b) Dispaly the sentences to the screen so that they appear exactly 
#     as shown above.
#################.
# ONE ANSWER 
#################.

# answer to part (a) - using backslashes
sentences = c("Sue said \"Hi\" to Joe.", "What's up with you?")
sentences
[1] "Sue said \"Hi\" to Joe." "What's up with you?"    
length(sentences)
[1] 2
# answer to part (b) 
cat(sentences, sep="\n")
Sue said "Hi" to Joe.
What's up with you?
#################.
# ANOTHER ANSWER
#################.

# answer to part (a) - no backslashes necessary
sentences = c('Sue said "Hi" to Joe.', "What's up with you?")
sentences
[1] "Sue said \"Hi\" to Joe." "What's up with you?"    
length(sentences)
[1] 2
# answer to part (b) - same as previous answer
cat(sentences, sep="\n")
Sue said "Hi" to Joe.
What's up with you?
# Question 18.
#
# Create a vector with the powers of 3 from 1 to 10. 
# This should get you ten numbers:
#    3 9 27 81 243 729 2187 6561 19683 59049
#
# Then create a vector with the products 30*1 30*2 ... 30*10
# This should get you ten numbers:
#    30 60 90 120 150 180 210 240 270 300
#
# Compare the first list of numbers to the second list of 
# numbers with the greater than sign. This should get the
# following logical vector 
# FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE
# (i.e. 4 FALSE values followed by 6 TRUE values)
#
# Explain in your own words why the result is
#    FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE
#############.
# ANSWER 
#############.
3^(1:10) > 30*(1:10)
 [1] FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
# The result will be comprised of TRUE/FALSE values since the 
# "greater than" operator i.e. > results in TRUE/FALSE values. 
#
# There will be 10 values in the result since each of the numeric vectors
# in the command has 10 values. 
#
# The result is FALSE wherever the 1st vector
# i.e. 3 9 27 81 243 729 2187 6561 19683 59049
# is not greater than the corresponding value in the 2nd vector
# i.e. 30 60 90 120 150 180 210 240 270 300
# The result is TRUE for every other position.
# Question 19.
# TOPICS: character vectors, nchar, rep, recycling rule between arguments of a single function
#
# charvec is a character vector.
# Write a command that repeats each value in charvec by as many characters 
# that are in that value. See the example below.
#
# EXAMPLE:
#
#    > charvec = c("a", "bc", "def")
#    > YOUR CODE GOES HERE
#    [1] "a"   "bc"  "bc"  "def" "def" "def"
###########.
# ANSWER
###########.

# setup some sample data
charvec = c("a", "bc", "def")

# answer
rep(charvec, nchar(charvec))
[1] "a"   "bc"  "bc"  "def" "def" "def"