37  Practice Questions - TOPIC: Vectors2

37.1 Vectors

QUESTION 1 (vectors, rep/seq/:/etc)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION:
#
# Write an R command that displays all the even numbers between 100 and 1000 (including 100 and 1000) 
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
seq(100,1000,by=2)  #<<ANSWER>>
  [1]  100  102  104  106  108  110  112  114  116  118  120  122  124  126  128
 [16]  130  132  134  136  138  140  142  144  146  148  150  152  154  156  158
 [31]  160  162  164  166  168  170  172  174  176  178  180  182  184  186  188
 [46]  190  192  194  196  198  200  202  204  206  208  210  212  214  216  218
 [61]  220  222  224  226  228  230  232  234  236  238  240  242  244  246  248
 [76]  250  252  254  256  258  260  262  264  266  268  270  272  274  276  278
 [91]  280  282  284  286  288  290  292  294  296  298  300  302  304  306  308
[106]  310  312  314  316  318  320  322  324  326  328  330  332  334  336  338
[121]  340  342  344  346  348  350  352  354  356  358  360  362  364  366  368
[136]  370  372  374  376  378  380  382  384  386  388  390  392  394  396  398
[151]  400  402  404  406  408  410  412  414  416  418  420  422  424  426  428
[166]  430  432  434  436  438  440  442  444  446  448  450  452  454  456  458
[181]  460  462  464  466  468  470  472  474  476  478  480  482  484  486  488
[196]  490  492  494  496  498  500  502  504  506  508  510  512  514  516  518
[211]  520  522  524  526  528  530  532  534  536  538  540  542  544  546  548
[226]  550  552  554  556  558  560  562  564  566  568  570  572  574  576  578
[241]  580  582  584  586  588  590  592  594  596  598  600  602  604  606  608
[256]  610  612  614  616  618  620  622  624  626  628  630  632  634  636  638
[271]  640  642  644  646  648  650  652  654  656  658  660  662  664  666  668
[286]  670  672  674  676  678  680  682  684  686  688  690  692  694  696  698
[301]  700  702  704  706  708  710  712  714  716  718  720  722  724  726  728
[316]  730  732  734  736  738  740  742  744  746  748  750  752  754  756  758
[331]  760  762  764  766  768  770  772  774  776  778  780  782  784  786  788
[346]  790  792  794  796  798  800  802  804  806  808  810  812  814  816  818
[361]  820  822  824  826  828  830  832  834  836  838  840  842  844  846  848
[376]  850  852  854  856  858  860  862  864  866  868  870  872  874  876  878
[391]  880  882  884  886  888  890  892  894  896  898  900  902  904  906  908
[406]  910  912  914  916  918  920  922  924  926  928  930  932  934  936  938
[421]  940  942  944  946  948  950  952  954  956  958  960  962  964  966  968
[436]  970  972  974  976  978  980  982  984  986  988  990  992  994  996  998
[451] 1000

QUESTION 2 (vectors , rep/seq/:/etc)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION:
#
# Write an R command that displays the values:   10, 13, 16, 19, 22, etc. There
# should be 500 numbers in all.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
seq(10,by=3,length.out=500)  #ANSWER
  [1]   10   13   16   19   22   25   28   31   34   37   40   43   46   49   52
 [16]   55   58   61   64   67   70   73   76   79   82   85   88   91   94   97
 [31]  100  103  106  109  112  115  118  121  124  127  130  133  136  139  142
 [46]  145  148  151  154  157  160  163  166  169  172  175  178  181  184  187
 [61]  190  193  196  199  202  205  208  211  214  217  220  223  226  229  232
 [76]  235  238  241  244  247  250  253  256  259  262  265  268  271  274  277
 [91]  280  283  286  289  292  295  298  301  304  307  310  313  316  319  322
[106]  325  328  331  334  337  340  343  346  349  352  355  358  361  364  367
[121]  370  373  376  379  382  385  388  391  394  397  400  403  406  409  412
[136]  415  418  421  424  427  430  433  436  439  442  445  448  451  454  457
[151]  460  463  466  469  472  475  478  481  484  487  490  493  496  499  502
[166]  505  508  511  514  517  520  523  526  529  532  535  538  541  544  547
[181]  550  553  556  559  562  565  568  571  574  577  580  583  586  589  592
[196]  595  598  601  604  607  610  613  616  619  622  625  628  631  634  637
[211]  640  643  646  649  652  655  658  661  664  667  670  673  676  679  682
[226]  685  688  691  694  697  700  703  706  709  712  715  718  721  724  727
[241]  730  733  736  739  742  745  748  751  754  757  760  763  766  769  772
[256]  775  778  781  784  787  790  793  796  799  802  805  808  811  814  817
[271]  820  823  826  829  832  835  838  841  844  847  850  853  856  859  862
[286]  865  868  871  874  877  880  883  886  889  892  895  898  901  904  907
[301]  910  913  916  919  922  925  928  931  934  937  940  943  946  949  952
[316]  955  958  961  964  967  970  973  976  979  982  985  988  991  994  997
[331] 1000 1003 1006 1009 1012 1015 1018 1021 1024 1027 1030 1033 1036 1039 1042
[346] 1045 1048 1051 1054 1057 1060 1063 1066 1069 1072 1075 1078 1081 1084 1087
[361] 1090 1093 1096 1099 1102 1105 1108 1111 1114 1117 1120 1123 1126 1129 1132
[376] 1135 1138 1141 1144 1147 1150 1153 1156 1159 1162 1165 1168 1171 1174 1177
[391] 1180 1183 1186 1189 1192 1195 1198 1201 1204 1207 1210 1213 1216 1219 1222
[406] 1225 1228 1231 1234 1237 1240 1243 1246 1249 1252 1255 1258 1261 1264 1267
[421] 1270 1273 1276 1279 1282 1285 1288 1291 1294 1297 1300 1303 1306 1309 1312
[436] 1315 1318 1321 1324 1327 1330 1333 1336 1339 1342 1345 1348 1351 1354 1357
[451] 1360 1363 1366 1369 1372 1375 1378 1381 1384 1387 1390 1393 1396 1399 1402
[466] 1405 1408 1411 1414 1417 1420 1423 1426 1429 1432 1435 1438 1441 1444 1447
[481] 1450 1453 1456 1459 1462 1465 1468 1471 1474 1477 1480 1483 1486 1489 1492
[496] 1495 1498 1501 1504 1507

QUESTION 3. (vectors , rep/seq/:/etc)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION
#
# Write an R command that displays alternating values of positive and
# negatives of 1,2,3 ie.:  1,2,3,-1,-2,-3,1,2,3,-1,-2,-3,1,2,3 ... etc.
# There should be a total of 60 numbers in all.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rep(c(1:3,-1:-3),length.out=60)   # ANSWER
 [1]  1  2  3 -1 -2 -3  1  2  3 -1 -2 -3  1  2  3 -1 -2 -3  1  2  3 -1 -2 -3  1
[26]  2  3 -1 -2 -3  1  2  3 -1 -2 -3  1  2  3 -1 -2 -3  1  2  3 -1 -2 -3  1  2
[51]  3 -1 -2 -3  1  2  3 -1 -2 -3
rep(c(1,2,3,-1,-2,-3),length.out=60)   # ANOTHER ANSWER
 [1]  1  2  3 -1 -2 -3  1  2  3 -1 -2 -3  1  2  3 -1 -2 -3  1  2  3 -1 -2 -3  1
[26]  2  3 -1 -2 -3  1  2  3 -1 -2 -3  1  2  3 -1 -2 -3  1  2  3 -1 -2 -3  1  2
[51]  3 -1 -2 -3  1  2  3 -1 -2 -3

QUESTION 4. (vectors , recycling)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 4    TOPICS: vectors , recycling  ####
# 
# Write an R command that displays all the powers of 2 from 
# 2 to the power of minus 20 (i.e. -20) until
# 2 to the power of positive 20 (i.e. +20)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-20:20      # this gives the numbers -20 -19 -18 ... -2 -1 0 1 2 ... 18 19 20
 [1] -20 -19 -18 -17 -16 -15 -14 -13 -12 -11 -10  -9  -8  -7  -6  -5  -4  -3  -2
[20]  -1   0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17
[39]  18  19  20
2^(-20:20)  # ANSWER
 [1] 9.536743e-07 1.907349e-06 3.814697e-06 7.629395e-06 1.525879e-05
 [6] 3.051758e-05 6.103516e-05 1.220703e-04 2.441406e-04 4.882812e-04
[11] 9.765625e-04 1.953125e-03 3.906250e-03 7.812500e-03 1.562500e-02
[16] 3.125000e-02 6.250000e-02 1.250000e-01 2.500000e-01 5.000000e-01
[21] 1.000000e+00 2.000000e+00 4.000000e+00 8.000000e+00 1.600000e+01
[26] 3.200000e+01 6.400000e+01 1.280000e+02 2.560000e+02 5.120000e+02
[31] 1.024000e+03 2.048000e+03 4.096000e+03 8.192000e+03 1.638400e+04
[36] 3.276800e+04 6.553600e+04 1.310720e+05 2.621440e+05 5.242880e+05
[41] 1.048576e+06
2^-20:20    # NOTE: This is WRONG because order of operations makes this work like (2^-20):20
 [1] 9.536743e-07 1.000001e+00 2.000001e+00 3.000001e+00 4.000001e+00
 [6] 5.000001e+00 6.000001e+00 7.000001e+00 8.000001e+00 9.000001e+00
[11] 1.000000e+01 1.100000e+01 1.200000e+01 1.300000e+01 1.400000e+01
[16] 1.500000e+01 1.600000e+01 1.700000e+01 1.800000e+01 1.900000e+01

QUESTION 5 (vectors , indexingWithLogicalValues)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 
#
# Write an R command that displays every entry in the vector x whose value is 10 or more.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
x <- c(3,10,11,2,-5,100,21)  # setup some example data
x
[1]   3  10  11   2  -5 100  21
x >= 10       # This gives TRUE/FALSE values - there will be one 
[1] FALSE  TRUE  TRUE FALSE FALSE  TRUE  TRUE
              # TRUE or FALSE for each value in x. Given the data above,
              # this will be: c(FALSE,TRUE,TRUE,FALSE,FALSE,TRUE,TRUE)

x[x>=10]      # ANSWER - The first "x" outside the [brackets] is the vector
[1]  10  11 100  21
              #          whose values are going to be displayed. The 
              #          "x>=10" inside the [brackets] is used to generate
              #          a logical vector (i.e. TRUE/FALSE values) to indicate
              #          which values will be displayed. Only those values
              #          from x whose positions correspond to a TRUE value
              #          in the logical vector, will be displayed. Given the
              #          sample data above, the 2nd, 3rd, 5th and 7th values
              #          will be displayed, i.e. 10,11,100,21

QUESTION 6 TOPICS: vectors , indexingWithLogicalValues

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 6    TOPICS: vectors , indexingWithLogicalValues ####
#
# x and y are two numeric vectors whose lengths are equal.
# Show every value from the vector, x, that is less than the corresponding value in the vector y
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
x <- c(3,10,11,2,-5,100,21)  # setup some example data 
y <- c(10,1,11,15,5,9,999)   # setup some example data
length(x)
[1] 7
length(y)
[1] 7
x < y         # This gives TRUE/FALSE values - there will be one 
[1]  TRUE FALSE FALSE  TRUE  TRUE FALSE  TRUE
              # TRUE or FALSE for each value in x. Given the data above,
              # this will be: c(FALSE,TRUE,TRUE,FALSE,FALSE,TRUE,TRUE)

x[x<y]     # ANSWER
[1]  3  2 -5 21
y[x<y]     # FYI - this wasn't asked but these are the corresponding
[1]  10  15   5 999
           # values from y that are greater than those in x

QUESTION 7 TOPICS: vectors , round , runif , RHelpPages

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 7    TOPICS: vectors , round , runif , RHelpPages  ####
#
# SOME BACKGROUND - runif function
#
#     The runif function returns a numeric vector that contains "random numbers
#     with a uniform distribution". The R help page for runif shows that the
#     arguments and default values for those arguments are:
#         runif(n, min=0, max=1)     
#     where
#         n is the number of numbers to generate
#         min is the smallest value that a number could have
#         max is the largest value that a number could have. 
#
#     EXAMPLES: 
#  
#     > # return a vector of 3 random numbers whose values fall between 0 and 10
#     > runif(3,0,10)   
#     [1] 3.785233 9.672244 2.857430
# 
#     > # return a vector of 10 random numbers whose values fall between -5 and +5
#     > runif(10,-5,5)
#     [1] -1.84032071  4.28685602  2.59656498 -1.09880205 -3.90202237 -1.89618831  3.37193245
#     [8]  0.08757248 -1.10848900 -1.90661883
# 
#     > # return a vector of 5 random numbers. The min and max are not specified so 
#     > # use the default values of min (ie. 0) and max (i.e. 1)
#     > runif(5)
#     [1] 0.4098594 0.8771700 0.5610475 0.8928674 0.4863841 
#
#   WHAT YOU HAVE TO DO:
#
#     Generate 10 random numbers that are between 50 and 51. The numbers should have
#     2 values after the decimal point. (HINT: use the   runif   and the   round
#     functions). Note that every time you run this command the actual numbers will
#     be different.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
runif(10,50,51)                  # generate 10 random numbers between 50 and 51
 [1] 50.85329 50.27708 50.23595 50.06822 50.64984 50.94805 50.86982 50.93077
 [9] 50.04514 50.85972
round(runif(10,50,51),digits=2)  # ANSWER 
 [1] 50.73 50.62 50.86 50.57 50.20 50.67 50.42 50.56 50.02 50.08

QUESTION 8 TOPICS: vectors , c

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 8    TOPICS: vectors , c ####
#
# Insert the number 5 in the beginning of the vector named x.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
x <- c(7,8,2,1)  # start with some data for x (this could be ANY data)
x <- c(5,x)      # ANSWER -  x is now c(5,7,8,2,1)
x             
[1] 5 7 8 2 1
x[1] <- 5    # WRONG: This will change the first position to 5
# given the example data above the 7 will be overwritten with the 5
# and x will be c(5,8,2,1)

QUESTION 9 TOPICS: vectors , c , length

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 9   TOPICS: vectors , c , length  ####
#
# Insert the number 6 at the end of the vector named x.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ONE WAY
x <- c(7,8,2,1)    # setup some example data
x <- c(x,6)    # ANSWER
x
[1] 7 8 2 1 6
# ANOTHER WAY
x <- c(7,8,2,1)    # setup some example data
x[length(x)+1] = 6   # ANOTHER ANSWER
x
[1] 7 8 2 1 6

QUESTION 10 TOPICS: vectors , c , indexingWithNumbers

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 10   TOPICS: vectors , c , indexingWithNumbers
#
# x is a vector that contains 11 or more values.
# Insert the number 7 between the 10th and 11th values in the vector named x.
#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
x <- seq(10,100,by=10)    # setup some example data - x is now:
x <- seq(10,150,by=10)    # setup some example data - x is now:
#   c(10,20,30,40,50,60,70,80,90,100,110,120,130,140,150)
x
 [1]  10  20  30  40  50  60  70  80  90 100 110 120 130 140 150
x <- c(x[1:10],7,x[11:length(x)])  # ANSWER 
x
 [1]  10  20  30  40  50  60  70  80  90 100   7 110 120 130 140 150
#   x is now :
#   c(10,20,30,40,50,60,70,80,90,100,7,110,120,130,140,150)
#
#   The "length(x)" is necessary so that this answer
#   will work no matter how many values the vector, x, contains.
#   the data above was just and example, but the command should 
#   now matter what values x contains. 

x
 [1]  10  20  30  40  50  60  70  80  90 100   7 110 120 130 140 150

QUESTION 11 TOPICS: vectors , c , indexingWithNumbers

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 11   TOPICS: vectors , c , indexingWithNumbers ####
#
# The vector x contains 10 values. Swap the first 5 and 
#     the last 5 values in x. For example, if x contains the 
#     values 1,2,3,4,5,6,7,8,9,10  then after your command runs, 
#     x should contain the values 6,7,8,9,10,1,2,3,4,5
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
x <- seq(10,100,by=10) # setup example data x contains c(10,20,30,40,50,60,70,80,90,100)

x
 [1]  10  20  30  40  50  60  70  80  90 100
x <- c(x[6:10],x[1:5]) # ANSWER -  x now contains c(60,70,80,90,100,10,20,30,40,50)
x
 [1]  60  70  80  90 100  10  20  30  40  50

QUESTION 12 TOPICS: namedVectors

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 12   TOPICS: namedVectors ####
#
# Grades is a named vector that contains test scores.
#     The names of the entries are the names of the students.
#     Exchange the value of Tom's grade with Joe's grade where
#     "tom" and "joe" are the names of the values that contain
#     the grades for Tom and Joe.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
grades <- c(90,80,70,100,85)                        # setup some example data: set the values in the vector
names(grades) <- c("sue","tom","bill","ann","joe")  # setup some example data: set the names of the values
names(grades)                                       # this displays just the names: "sue","tom",etc.
[1] "sue"  "tom"  "bill" "ann"  "joe" 
grades                                              # this displays the values:  90,80,70,100,85
 sue  tom bill  ann  joe 
  90   80   70  100   85 
grades[c("tom","joe")]                              # this displays tom's grade and joe's grade: 80  85
tom joe 
 80  85 
grades[c("joe","tom")]                              # this displays joe's grade and tom's grade: 85  80
joe tom 
 85  80 
grades[c("tom","joe")] <- grades[c("joe","tom")]  # ANSWER: this puts the value of 
# Joe's grade into the position for Tom's grade and
# Tom's grade into the position for Joe's grade.
grades                                              # show the new vector
 sue  tom bill  ann  joe 
  90   85   70  100   80 

QUESTION 13 TOPICS: namedVectors , indexingWithLogicalValues

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 13   TOPICS: namedVectors , indexingWithLogicalValues ####
#
# Grades is a named vector that contains test scores. The names 
# of the entries are the names of the students. Change susan's name to sue
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
grades <- c(90,80,70,100,85)
names(grades) <- c("susan","tom","bill","ann","joe")
grades
susan   tom  bill   ann   joe 
   90    80    70   100    85 
names(grades)=="susan"   # Shows TRUE/FALSE values
[1]  TRUE FALSE FALSE FALSE FALSE
names(grades) [ names(grades)=="susan" ]  # identify item in the names vector that contains susan
[1] "susan"
# The following line is the ONLY line that is required for the answer.
# The code puts the value "sue" into the position of the vector, names(grades)
# that currently contains the value "susan"

names(grades) [ names(grades)=="susan" ] <- "sue"       # ANSWER 

grades
 sue  tom bill  ann  joe 
  90   80   70  100   85 

QUESTION 14 TOPICS: vectors , indexingWithNumbers , vectorAssignment

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 14   TOPICS: vectors , indexingWithNumbers , vectorAssignment ####
#
# Exchange the 4th and 7th value in the vector nums.
#
#      a.  Do this using 3 different commands. Hint, you may create new variables. 
#          (Hint, you may create new variables. For example, I'll start you off:
#              fourth <- x[4]
#              seventh <- x[7]
#              # fill in the rest of the R code to answer the question.
#
#      b. Do this using only a single R command
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#14a
nums <- seq(10,100,by=10)
x <- nums[7]        # ANSWER (MULTIPLE LINES)
nums[7] <- nums[4]  # ANSWER (MULTIPLE LINES)
nums[4] <- x        # ANSWER (MULTIPLE LINES)
nums
 [1]  10  20  30  70  50  60  40  80  90 100
#14b
nums <- seq(10,100,by=10)
nums[c(4,7)] <- nums[c(7,4)]   # ANSWER
nums
 [1]  10  20  30  70  50  60  40  80  90 100

QUESTION 15 TOPICS: vectors , indexingWithNumbers , length

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 15   TOPICS: vectors , indexingWithNumbers , length
#
# Display the last value from the vector x.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
x <- c(10,20,30,40,50)
x[length(x)]    # ANSWER
[1] 50

QUESTION 16 TOPICS: vectors , indexingWithNumbers , length

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 16   TOPICS: vectors , indexingWithNumbers , length
#
# Display the 2nd to last value in x.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
x <- c(10,20,30,40,50)
x[length(x)-1]          # ANSWER
[1] 40

QUESTION 17 TOPICS: vectors , indexingWithNumbers , mean , orderOfOperations

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 17   TOPICS: vectors , indexingWithNumbers , mean , orderOfOperations ####
#
# Display the average of the last 5 values in x.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
x <- c(1,5,2,3,1,20,4,10,6,50)
mean(x[(length(x)-4):length(x)])   # ANSWER
[1] 18
# Note, in the answer above, notice the parentheses around (length(x)-4).
# These parentheses are REQUIRED due to order of operations. Without the parentheses
# the command, length(x)-4:length(x) would actually be interpreted by R as 
# length(x)-(4:length(x)) which would give the WRONG result. For more information, 
# see the "recipe" about "order of operations in R" in the "R Cookbook" published by O'Reilly

QUESTION 18a TOPICS: vectors , vectorArithmetic

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 18a
# TOPICS: vectors, vectorArithmetic, seqColon, indexingWithLogicalValues, length, sciNotation, Inf
#
# Inf is used for "Infinity"
# 
# R can only work with numbers up to a certain size. Any value that is more
# than the maximum is returned as Inf (which stands for Infinity).
#
# Example:
#
# SCIENTIFIC NOTATION
#
# By default R displays very large and very small numbers using a mathematical
# notation called "scientific notation". "Scientific notation" is just a
# fancy way of writing very large and very small numbers using very little
# typing.
#
# To understand "scientific notation", let's start with a simple example.
# 1e+3 is the scientific notation way to write the number 1000.
# 1e+3 can be understood as 1*10^3 (as explained in the next paragraph)
#
# People who are not familiar with scientific notation might read
# "1e+3" as "one e plus 3". However, that is NOT the right way to read it.
# Instead, whenever you see "e" in scientific notation, read it as though
# instead of "e" it said "... times 10 to the power of ...", so "1e+3" should
# be read as "one times 10 to the power of positive 3"
# i.e., "1e+3" is the same as 1*10^3. 
# ( By the way, the letter "e" is used because, "e" is short for "exponent". )
#
# In a similar way, 2.3e+12 is the scientific notation way of writing the
# number 2300000000000 which is the same as "2.3 times 10 to the power of 12"
# i.e. 2.3*10^12
#
# To see that R "understands" scientific notation, just type 1e+3 into R.
# Because 1e+3 is the same as 1000, R just displays 1000. For example:
#
#   > 1e+3      # this is scientific notation for 1000.
#   [1] 1000
#
# By default if a number is small enough then R displays it in it's "regular"
# form (i.e. NOT in scientific notation). However, if a number is very large or
# extremely small, then R displays the number in scientific notation:
#
#   > # "regular numbers" get displayed normally
#   > 1000      
#   [1] 1000
#
#   > # very large numbers get displayed in scientific notation (with a positive exponent)
#   > 1000000000000  
#   [1] 1e+12
#
#   > # extremely small numbers get displayed in scientific notation (with a negative exponent)
#   > 0.0000000000001   
#   [1] 1e-13
#
# -----------------------------------------------------------------------------
# Write code that answers the following question:
#
#    What is the highest power of 4 that is less than one million?
#
# Your code should produce the answer 9. 
#
# The answer is 9 since 4^9=262,144 which is less than 1 million
# but 4^10=1,048,576 is more than one million.
# 
# Write R code to calculate this answer (i.e. 9) automatically.
# Your code should work for any base that is between 2 and 1000 (because 1000 is the
# square root of one million), not only for a base of 4. For example, if instead of
# the number 4, your code used the variable "n", your code should print the
# highest power of "n" that is less than one million. You may write the answer
# using several lines of R code. 
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
n <- 4               # ANSWER (MULTIPLE LINES)
n2 <- n^(1:1000)     # ANSWER (MULTIPLE LINES)
n2
   [1]  4.000000e+00  1.600000e+01  6.400000e+01  2.560000e+02  1.024000e+03
   [6]  4.096000e+03  1.638400e+04  6.553600e+04  2.621440e+05  1.048576e+06
  [11]  4.194304e+06  1.677722e+07  6.710886e+07  2.684355e+08  1.073742e+09
  [16]  4.294967e+09  1.717987e+10  6.871948e+10  2.748779e+11  1.099512e+12
  [21]  4.398047e+12  1.759219e+13  7.036874e+13  2.814750e+14  1.125900e+15
  [26]  4.503600e+15  1.801440e+16  7.205759e+16  2.882304e+17  1.152922e+18
  [31]  4.611686e+18  1.844674e+19  7.378698e+19  2.951479e+20  1.180592e+21
  [36]  4.722366e+21  1.888947e+22  7.555786e+22  3.022315e+23  1.208926e+24
  [41]  4.835703e+24  1.934281e+25  7.737125e+25  3.094850e+26  1.237940e+27
  [46]  4.951760e+27  1.980704e+28  7.922816e+28  3.169127e+29  1.267651e+30
  [51]  5.070602e+30  2.028241e+31  8.112964e+31  3.245186e+32  1.298074e+33
  [56]  5.192297e+33  2.076919e+34  8.307675e+34  3.323070e+35  1.329228e+36
  [61]  5.316912e+36  2.126765e+37  8.507059e+37  3.402824e+38  1.361129e+39
  [66]  5.444518e+39  2.177807e+40  8.711229e+40  3.484491e+41  1.393797e+42
  [71]  5.575186e+42  2.230075e+43  8.920298e+43  3.568119e+44  1.427248e+45
  [76]  5.708991e+45  2.283596e+46  9.134385e+46  3.653754e+47  1.461502e+48
  [81]  5.846007e+48  2.338403e+49  9.353610e+49  3.741444e+50  1.496578e+51
  [86]  5.986311e+51  2.394524e+52  9.578097e+52  3.831239e+53  1.532496e+54
  [91]  6.129982e+54  2.451993e+55  9.807971e+55  3.923189e+56  1.569275e+57
  [96]  6.277102e+57  2.510841e+58  1.004336e+59  4.017345e+59  1.606938e+60
 [101]  6.427752e+60  2.571101e+61  1.028440e+62  4.113761e+62  1.645505e+63
 [106]  6.582018e+63  2.632807e+64  1.053123e+65  4.212492e+65  1.684997e+66
 [111]  6.739987e+66  2.695995e+67  1.078398e+68  4.313591e+68  1.725437e+69
 [116]  6.901746e+69  2.760699e+70  1.104279e+71  4.417118e+71  1.766847e+72
 [121]  7.067388e+72  2.826955e+73  1.130782e+74  4.523128e+74  1.809251e+75
 [126]  7.237006e+75  2.894802e+76  1.157921e+77  4.631684e+77  1.852673e+78
 [131]  7.410694e+78  2.964277e+79  1.185711e+80  4.742844e+80  1.897138e+81
 [136]  7.588550e+81  3.035420e+82  1.214168e+83  4.856672e+83  1.942669e+84
 [141]  7.770676e+84  3.108270e+85  1.243308e+86  4.973232e+86  1.989293e+87
 [146]  7.957172e+87  3.182869e+88  1.273147e+89  5.092590e+89  2.037036e+90
 [151]  8.148144e+90  3.259258e+91  1.303703e+92  5.214812e+92  2.085925e+93
 [156]  8.343699e+93  3.337480e+94  1.334992e+95  5.339968e+95  2.135987e+96
 [161]  8.543948e+96  3.417579e+97  1.367032e+98  5.468127e+98  2.187251e+99
 [166]  8.749003e+99 3.499601e+100 1.399840e+101 5.599362e+101 2.239745e+102
 [171] 8.958979e+102 3.583592e+103 1.433437e+104 5.733747e+104 2.293499e+105
 [176] 9.173994e+105 3.669598e+106 1.467839e+107 5.871356e+107 2.348543e+108
 [181] 9.394170e+108 3.757668e+109 1.503067e+110 6.012269e+110 2.404908e+111
 [186] 9.619630e+111 3.847852e+112 1.539141e+113 6.156563e+113 2.462625e+114
 [191] 9.850502e+114 3.940201e+115 1.576080e+116 6.304321e+116 2.521728e+117
 [196] 1.008691e+118 4.034765e+118 1.613906e+119 6.455625e+119 2.582250e+120
 [201] 1.032900e+121 4.131600e+121 1.652640e+122 6.610560e+122 2.644224e+123
 [206] 1.057690e+124 4.230758e+124 1.692303e+125 6.769213e+125 2.707685e+126
 [211] 1.083074e+127 4.332296e+127 1.732919e+128 6.931674e+128 2.772670e+129
 [216] 1.109068e+130 4.436272e+130 1.774509e+131 7.098034e+131 2.839214e+132
 [221] 1.135686e+133 4.542742e+133 1.817097e+134 7.268387e+134 2.907355e+135
 [226] 1.162942e+136 4.651768e+136 1.860707e+137 7.442829e+137 2.977131e+138
 [231] 1.190853e+139 4.763410e+139 1.905364e+140 7.621456e+140 3.048583e+141
 [236] 1.219433e+142 4.877732e+142 1.951093e+143 7.804371e+143 3.121749e+144
 [241] 1.248699e+145 4.994798e+145 1.997919e+146 7.991676e+146 3.196671e+147
 [246] 1.278668e+148 5.114673e+148 2.045869e+149 8.183477e+149 3.273391e+150
 [251] 1.309356e+151 5.237425e+151 2.094970e+152 8.379880e+152 3.351952e+153
 [256] 1.340781e+154 5.363123e+154 2.145249e+155 8.580997e+155 3.432399e+156
 [261] 1.372960e+157 5.491838e+157 2.196735e+158 8.786941e+158 3.514776e+159
 [266] 1.405911e+160 5.623642e+160 2.249457e+161 8.997828e+161 3.599131e+162
 [271] 1.439652e+163 5.758610e+163 2.303444e+164 9.213775e+164 3.685510e+165
 [276] 1.474204e+166 5.896816e+166 2.358727e+167 9.434906e+167 3.773962e+168
 [281] 1.509585e+169 6.038340e+169 2.415336e+170 9.661344e+170 3.864538e+171
 [286] 1.545815e+172 6.183260e+172 2.473304e+173 9.893216e+173 3.957286e+174
 [291] 1.582915e+175 6.331658e+175 2.532663e+176 1.013065e+177 4.052261e+177
 [296] 1.620905e+178 6.483618e+178 2.593447e+179 1.037379e+180 4.149516e+180
 [301] 1.659806e+181 6.639225e+181 2.655690e+182 1.062276e+183 4.249104e+183
 [306] 1.699642e+184 6.798566e+184 2.719427e+185 1.087771e+186 4.351082e+186
 [311] 1.740433e+187 6.961732e+187 2.784693e+188 1.113877e+189 4.455508e+189
 [316] 1.782203e+190 7.128813e+190 2.851525e+191 1.140610e+192 4.562441e+192
 [321] 1.824976e+193 7.299905e+193 2.919962e+194 1.167985e+195 4.671939e+195
 [326] 1.868776e+196 7.475103e+196 2.990041e+197 1.196016e+198 4.784066e+198
 [331] 1.913626e+199 7.654505e+199 3.061802e+200 1.224721e+201 4.898883e+201
 [336] 1.959553e+202 7.838213e+202 3.135285e+203 1.254114e+204 5.016457e+204
 [341] 2.006583e+205 8.026330e+205 3.210532e+206 1.284213e+207 5.136851e+207
 [346] 2.054741e+208 8.218962e+208 3.287585e+209 1.315034e+210 5.260136e+210
 [351] 2.104054e+211 8.416217e+211 3.366487e+212 1.346595e+213 5.386379e+213
 [356] 2.154552e+214 8.618207e+214 3.447283e+215 1.378913e+216 5.515652e+216
 [361] 2.206261e+217 8.825044e+217 3.530017e+218 1.412007e+219 5.648028e+219
 [366] 2.259211e+220 9.036845e+220 3.614738e+221 1.445895e+222 5.783581e+222
 [371] 2.313432e+223 9.253729e+223 3.701492e+224 1.480597e+225 5.922387e+225
 [376] 2.368955e+226 9.475818e+226 3.790327e+227 1.516131e+228 6.064524e+228
 [381] 2.425810e+229 9.703238e+229 3.881295e+230 1.552518e+231 6.210072e+231
 [386] 2.484029e+232 9.936116e+232 3.974446e+233 1.589779e+234 6.359114e+234
 [391] 2.543646e+235 1.017458e+236 4.069833e+236 1.627933e+237 6.511733e+237
 [396] 2.604693e+238 1.041877e+239 4.167509e+239 1.667004e+240 6.668014e+240
 [401] 2.667206e+241 1.066882e+242 4.267529e+242 1.707012e+243 6.828047e+243
 [406] 2.731219e+244 1.092487e+245 4.369950e+245 1.747980e+246 6.991920e+246
 [411] 2.796768e+247 1.118707e+248 4.474829e+248 1.789931e+249 7.159726e+249
 [416] 2.863890e+250 1.145556e+251 4.582225e+251 1.832890e+252 7.331559e+252
 [421] 2.932624e+253 1.173050e+254 4.692198e+254 1.876879e+255 7.507517e+255
 [426] 3.003007e+256 1.201203e+257 4.804811e+257 1.921924e+258 7.687697e+258
 [431] 3.075079e+259 1.230032e+260 4.920126e+260 1.968050e+261 7.872202e+261
 [436] 3.148881e+262 1.259552e+263 5.038209e+263 2.015284e+264 8.061135e+264
 [441] 3.224454e+265 1.289782e+266 5.159126e+266 2.063651e+267 8.254602e+267
 [446] 3.301841e+268 1.320736e+269 5.282945e+269 2.113178e+270 8.452712e+270
 [451] 3.381085e+271 1.352434e+272 5.409736e+272 2.163894e+273 8.655578e+273
 [456] 3.462231e+274 1.384892e+275 5.539570e+275 2.215828e+276 8.863311e+276
 [461] 3.545325e+277 1.418130e+278 5.672519e+278 2.269008e+279 9.076031e+279
 [466] 3.630412e+280 1.452165e+281 5.808660e+281 2.323464e+282 9.293856e+282
 [471] 3.717542e+283 1.487017e+284 5.948068e+284 2.379227e+285 9.516908e+285
 [476] 3.806763e+286 1.522705e+287 6.090821e+287 2.436329e+288 9.745314e+288
 [481] 3.898126e+289 1.559250e+290 6.237001e+290 2.494800e+291 9.979202e+291
 [486] 3.991681e+292 1.596672e+293 6.386689e+293 2.554676e+294 1.021870e+295
 [491] 4.087481e+295 1.634992e+296 6.539970e+296 2.615988e+297 1.046395e+298
 [496] 4.185580e+298 1.674232e+299 6.696929e+299 2.678772e+300 1.071509e+301
 [501] 4.286034e+301 1.714414e+302 6.857655e+302 2.743062e+303 1.097225e+304
 [506] 4.388899e+304 1.755560e+305 7.022239e+305 2.808896e+306 1.123558e+307
 [511] 4.494233e+307           Inf           Inf           Inf           Inf
 [516]           Inf           Inf           Inf           Inf           Inf
 [521]           Inf           Inf           Inf           Inf           Inf
 [526]           Inf           Inf           Inf           Inf           Inf
 [531]           Inf           Inf           Inf           Inf           Inf
 [536]           Inf           Inf           Inf           Inf           Inf
 [541]           Inf           Inf           Inf           Inf           Inf
 [546]           Inf           Inf           Inf           Inf           Inf
 [551]           Inf           Inf           Inf           Inf           Inf
 [556]           Inf           Inf           Inf           Inf           Inf
 [561]           Inf           Inf           Inf           Inf           Inf
 [566]           Inf           Inf           Inf           Inf           Inf
 [571]           Inf           Inf           Inf           Inf           Inf
 [576]           Inf           Inf           Inf           Inf           Inf
 [581]           Inf           Inf           Inf           Inf           Inf
 [586]           Inf           Inf           Inf           Inf           Inf
 [591]           Inf           Inf           Inf           Inf           Inf
 [596]           Inf           Inf           Inf           Inf           Inf
 [601]           Inf           Inf           Inf           Inf           Inf
 [606]           Inf           Inf           Inf           Inf           Inf
 [611]           Inf           Inf           Inf           Inf           Inf
 [616]           Inf           Inf           Inf           Inf           Inf
 [621]           Inf           Inf           Inf           Inf           Inf
 [626]           Inf           Inf           Inf           Inf           Inf
 [631]           Inf           Inf           Inf           Inf           Inf
 [636]           Inf           Inf           Inf           Inf           Inf
 [641]           Inf           Inf           Inf           Inf           Inf
 [646]           Inf           Inf           Inf           Inf           Inf
 [651]           Inf           Inf           Inf           Inf           Inf
 [656]           Inf           Inf           Inf           Inf           Inf
 [661]           Inf           Inf           Inf           Inf           Inf
 [666]           Inf           Inf           Inf           Inf           Inf
 [671]           Inf           Inf           Inf           Inf           Inf
 [676]           Inf           Inf           Inf           Inf           Inf
 [681]           Inf           Inf           Inf           Inf           Inf
 [686]           Inf           Inf           Inf           Inf           Inf
 [691]           Inf           Inf           Inf           Inf           Inf
 [696]           Inf           Inf           Inf           Inf           Inf
 [701]           Inf           Inf           Inf           Inf           Inf
 [706]           Inf           Inf           Inf           Inf           Inf
 [711]           Inf           Inf           Inf           Inf           Inf
 [716]           Inf           Inf           Inf           Inf           Inf
 [721]           Inf           Inf           Inf           Inf           Inf
 [726]           Inf           Inf           Inf           Inf           Inf
 [731]           Inf           Inf           Inf           Inf           Inf
 [736]           Inf           Inf           Inf           Inf           Inf
 [741]           Inf           Inf           Inf           Inf           Inf
 [746]           Inf           Inf           Inf           Inf           Inf
 [751]           Inf           Inf           Inf           Inf           Inf
 [756]           Inf           Inf           Inf           Inf           Inf
 [761]           Inf           Inf           Inf           Inf           Inf
 [766]           Inf           Inf           Inf           Inf           Inf
 [771]           Inf           Inf           Inf           Inf           Inf
 [776]           Inf           Inf           Inf           Inf           Inf
 [781]           Inf           Inf           Inf           Inf           Inf
 [786]           Inf           Inf           Inf           Inf           Inf
 [791]           Inf           Inf           Inf           Inf           Inf
 [796]           Inf           Inf           Inf           Inf           Inf
 [801]           Inf           Inf           Inf           Inf           Inf
 [806]           Inf           Inf           Inf           Inf           Inf
 [811]           Inf           Inf           Inf           Inf           Inf
 [816]           Inf           Inf           Inf           Inf           Inf
 [821]           Inf           Inf           Inf           Inf           Inf
 [826]           Inf           Inf           Inf           Inf           Inf
 [831]           Inf           Inf           Inf           Inf           Inf
 [836]           Inf           Inf           Inf           Inf           Inf
 [841]           Inf           Inf           Inf           Inf           Inf
 [846]           Inf           Inf           Inf           Inf           Inf
 [851]           Inf           Inf           Inf           Inf           Inf
 [856]           Inf           Inf           Inf           Inf           Inf
 [861]           Inf           Inf           Inf           Inf           Inf
 [866]           Inf           Inf           Inf           Inf           Inf
 [871]           Inf           Inf           Inf           Inf           Inf
 [876]           Inf           Inf           Inf           Inf           Inf
 [881]           Inf           Inf           Inf           Inf           Inf
 [886]           Inf           Inf           Inf           Inf           Inf
 [891]           Inf           Inf           Inf           Inf           Inf
 [896]           Inf           Inf           Inf           Inf           Inf
 [901]           Inf           Inf           Inf           Inf           Inf
 [906]           Inf           Inf           Inf           Inf           Inf
 [911]           Inf           Inf           Inf           Inf           Inf
 [916]           Inf           Inf           Inf           Inf           Inf
 [921]           Inf           Inf           Inf           Inf           Inf
 [926]           Inf           Inf           Inf           Inf           Inf
 [931]           Inf           Inf           Inf           Inf           Inf
 [936]           Inf           Inf           Inf           Inf           Inf
 [941]           Inf           Inf           Inf           Inf           Inf
 [946]           Inf           Inf           Inf           Inf           Inf
 [951]           Inf           Inf           Inf           Inf           Inf
 [956]           Inf           Inf           Inf           Inf           Inf
 [961]           Inf           Inf           Inf           Inf           Inf
 [966]           Inf           Inf           Inf           Inf           Inf
 [971]           Inf           Inf           Inf           Inf           Inf
 [976]           Inf           Inf           Inf           Inf           Inf
 [981]           Inf           Inf           Inf           Inf           Inf
 [986]           Inf           Inf           Inf           Inf           Inf
 [991]           Inf           Inf           Inf           Inf           Inf
 [996]           Inf           Inf           Inf           Inf           Inf
n2 < 1000000
   [1]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE
  [13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
  [25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
  [37] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
  [49] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
  [61] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
  [73] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
  [85] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
  [97] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [109] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [121] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [133] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [145] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [157] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [169] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [181] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [193] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [205] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [217] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [229] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [241] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [253] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [265] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [277] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [289] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [301] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [313] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [325] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [337] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [349] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [361] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [373] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [385] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [397] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [409] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [421] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [433] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [445] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [457] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [469] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [481] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [493] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [505] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [517] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [529] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [541] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [553] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [565] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [577] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [589] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [601] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [613] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [625] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [637] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [649] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [661] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [673] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [685] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [697] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [709] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [721] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [733] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [745] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [757] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [769] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [781] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [793] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [805] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [817] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [829] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [841] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [853] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [865] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [877] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [889] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [901] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [913] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [925] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [937] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [949] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [961] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [973] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [985] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [997] FALSE FALSE FALSE FALSE
n2 <- n2[n2<1000000] # ANSWER (MULTIPLE LINES)
n2
[1]      4     16     64    256   1024   4096  16384  65536 262144
length(n2)           # ANSWER (MULTIPLE LINES) (this will display the number 9)
[1] 9

QUESTION 18b TOPICS: vectors

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 18b
# TOPICS: vectors, vectorArithmetic, seqColon, indexingWithLogicalValues, length
#
# Same as previous question, but this time n contains a "base" as before and
# x contains the maximum value (In the previous question this was one million.
# In this question it can be any number that is contained in x.) The question
# now is what is the maximum whole-number power that n can be raised to whose
# result is less than x.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
x <- 200
n <- 3               # ANSWER (MULTIPLE LINES)
n2 <- n^(1:sqrt(x))  # ANSWER (MULTIPLE LINES)
n2 <- n2[n2<x]       # ANSWER (MULTIPLE LINES)
length(n2)           # ANSWER (MULTIPLE LINES)
[1] 4

QUESTION 19 TOPICS: vectors

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 19
# TOPICS: vectors, seqColon, indexingWithNumbers, vectorArithmetic
#
# nums is a vector that contains 100 numbers. 
#
# Use nums to create a vector that contains 50 numbers, as described below. 
# The first value should be the sum of the first two numbers from nums. 
# The 2nd value should be the sum of the 3rd and 4th values from nums. 
# For example, if nums contains 1,2,3,4,5,6 ... 99,100, then your answer 
# should contain 3,7,11 ... 199. 
#
# For example, if nums contains
#     3,6,2,8,1,-2, ... ninety-one-other-numbers ... 8,5,-5,2
#
# then your answer should contain
#     9,10,-1, ... forty-five-other-numbers ... 13,-3. 
#
# You may write the answer using several lines of R code.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nums <- 1:100
firstThirdFifthEtc <- nums[seq(1,100,by=2)]   # ANSWER (MULTIPLE LINES)
secondFourthSixthEtc <- nums[seq(2,100,by=2)]     # ANSWER (MULTIPLE LINES)
answer <- firstThirdFifthEtc + secondFourthSixthEtc  # ANSWER (MULTIPLE LINES)
length(answer)
[1] 50
firstThirdFifthEtc
 [1]  1  3  5  7  9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
[26] 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
secondFourthSixthEtc
 [1]   2   4   6   8  10  12  14  16  18  20  22  24  26  28  30  32  34  36  38
[20]  40  42  44  46  48  50  52  54  56  58  60  62  64  66  68  70  72  74  76
[39]  78  80  82  84  86  88  90  92  94  96  98 100
answer
 [1]   3   7  11  15  19  23  27  31  35  39  43  47  51  55  59  63  67  71  75
[20]  79  83  87  91  95  99 103 107 111 115 119 123 127 131 135 139 143 147 151
[39] 155 159 163 167 171 175 179 183 187 191 195 199

QUESTION 20 TOPICS: vectors

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 20
# TOPICS: vectors, ==and!=, indexingWithLogicalValues, seqColon
#
# x and y are vectors that each contain 5 numbers. 
#
# PART A:   Show the values from x whose corresponding values in y are the same.
# PART B:   Show the position numbers that contain the same values in x and in y.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#20a
x <- c(30,50,-20,10,40) # setup some example data
y <- c(99,50,-20,88,40) # setup some example data
x == y                  # This generates a vector that contains five TRUE/FALSE values
[1] FALSE  TRUE  TRUE FALSE  TRUE
x[x==y]                 # ANSWER - "x==y" generates a logical vector (i.e. TRUE/FALSE values).
[1]  50 -20  40
#          There will be one TRUE or FALSE value for each pair of values
#          in x and y. The "x" outside of the [brackets] indicates the 
#          vector whose values will be displayed. The values from x
#          that occupy the same positions as the TRUE values from "x==y"
#          will be displayed. The values from x that occupy the same positions
#          as the FALSE values from "x==y" will NOT be displayed.


#20b
x <- c(30,50,-20,10,40) # setup some example data
y <- c(99,50,-20,88,40) # setup some example data

(1:length(x))[x==y]     # ANSWER - NOTE: "x==y" generates a logical vector (i.e. TRUE/FALSE values).
[1] 2 3 5
#          There will be one TRUE or FALSE value for each pair of values
#          in x and y.
# 
#          The code: "(1:length(x))" that appears outside the [brackets]
#          generates a vector whose values are:
# 
#                   1  2  3  ... etc (up to the length of x).                        # 
#          Given the data above, x contains 5 values. Therefore 
#          the value of (1:length(x))will be:    1  2  3  4  5
# 
#          The code "[x==y]" is used to specify which of the values from the vector
#          should be displayed. The values from "(1:length(x))"
#          that occupy the same positions as the TRUE values from "x==y"
#          will be displayed. The values from x that occupy the same positions
#          as the FALSE values from "x==y" will NOT be displayed.

QUESTION 21 TOPICS: vectors, sumMean, indexingWithLogicalValues

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 21
# TOPICS: vectors, sumMean, indexingWithLogicalValues
#
# Show all values from the vector x that are greater than the average of the values in x. 
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
x <- c(30,50,-20,10,40)
mean(x)
[1] 22
x[x > mean(x)]    # ANSWER
[1] 30 50 40

QUESTION 22 TOPICS: vectors, sumMean, roundTruncCeilingFloor

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 22
# TOPICS: vectors, sumMean, roundTruncCeilingFloor
#
# The sd() function returns the standard deviation of a vector.
#    Show all of the numbers in the vector x that are at least 1.5 standard deviations
#    away from the mean of x. You can show the numbers in any order you like. They do not
#    have to be in the same order that they appear in the vector x.
#
# HINT:  Standard Deviation is a concept that is covered in statistics. If you didn't take
# stats yet, I can understand why you're confused.  Bottom line, is that sd(c(3,10,76,32,23,31))
# will return a SINGLE number that is called the "standard deviation" of the values
# 3,10,76,32,23,31. sd(c(3,10,76,32,23,31) is 25.67. The mean of those same values
# is 29.16.  Therefore, numbers that are at least 1.5 standard deviations away from the mean are 
#    greater than 29.16 + 1.5 * 25.67    = 67.67
#    or less than 29.16 - 1.5 * 25.67    =  -9.34
# Therefore, if x contained the numbers shown, then you should only display the number 76 as
# that is the only number that is outside of the specified range.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
x <- floor(runif(50,1,100))
x
 [1] 68 88 49 86 97 90 34 96 13 36 45 59 29 98 65 81 90 86 83 65 65  8 57 72 72
[26]  3  6 98 51  4 45 87 86 39  6 13 98 63 52 96 39  1 99 84 46 83 85  9 13 64
sd(x)
[1] 31.86769
mean(x)
[1] 58.04
mean(x) - 1.5 * sd(x)
[1] 10.23847
mean(x) + 1.5 * sd(x)
[1] 105.8415
x[x <= mean(x) - 1.5 * sd(x)]
[1] 8 3 6 4 6 1 9
x[x >= mean(x) + 1.5 * sd(x)]
numeric(0)
c ( x[x <= mean(x) - 1.5 * sd(x)]  ,  x[x >= mean(x) + 1.5 * sd(x)]  )    # ANSWER
[1] 8 3 6 4 6 1 9

QUESTION 23 TOPICS: vectors, parallelVectors

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 23
# TOPICS: vectors, parallelVectors
#
# The following variables are parallel vectors that contain info
# about 4 different rectangles.
#
#   colors  <- c("red","blue","green","yellow)
#   lengths <- c(4,5,6,7)
#   widths  <- c(5,6,2,4)
#            
# In other words:
#   the 1st rectangle is "red",    it's length is 4 and it's width is 5
#
#   the 2nd rectangle is "blue",   it's length is 5 and it's width is 6
#
#   the 3rd rectangle is "green",  it's length is 6 and it's width is 2
#
#   the 4th rectangle is "yellow", it's length is 7 and it's width is 4
#
# QUESTIONS:
#
#    The actual numbers and colors shown above are just an example.
#    Your answers must work even if the vectors contain different 
#    numbers and colors and even if the vectors contains fewer or more values.
#
#    a. Show the AREAS of all the rectangles.
#
#            Given the example data above, 
#            your code should evaluate to: 20  30  12 28
#
#    b. Show the COLORS of the rectangles whose areas are at least 25.
# 
#            Given the example data above, 
#            your code should evaluate to: "blue"  "yellow"
#
#    c. Show the COLORS of the rectangles 
#       whose length is at least 5
#       or whose width is at least 5.
#
#           Given the example data above,
#           your code should evaluate to: "red"  blue"  "green" "yellow"
#
#    d. Show the COLORS of the rectangles 
#       whose length and width are both at least 5.
#
#           Given the example data above,
#           your code should evaluate to: "blue"
# 
#    e. Show the COLORS of the rectangles whose area is at least 25.
#
#           Given the example data above,
#           your code should evaluate to: "blue"  "yellow"
# 
#    f. Show the LENGTHS of the blue rectangles whose area is at least 25
#
#           Given the example data above,
#           your code should evaluate to: 5
# 
#    g. Show the LENGTHS of the rectangles whose area is at least 25 
#       and whose color is any one of blue, green or yellow.
#
#           Given the example data above,
#           your code should evaluate to: 5 7
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 23a.

# setup some sample data as described in the question
lengths <- c(4,5,6,7)
widths <- c(5,6,2,4)
colors <- c("red","blue","green","yellow")

lengths * widths                          # ANSWER
[1] 20 30 12 28
# 23b. 

# setup some sample data as described in the question
lengths <- c(4,5,6,7)
widths <- c(5,6,2,4)
colors <- c("red","blue","green","yellow")

colors[(lengths * widths) >= 25]           # ANSWER
[1] "blue"   "yellow"
# 23c.

# setup some sample data as described in the question
lengths <- c(4,5,6,7)
widths <- c(5,6,2,4)
colors <- c("red","blue","green","yellow")

colors[lengths>=5 | widths>=5]           # ANSWER
[1] "red"    "blue"   "green"  "yellow"
# 23d.

# setup some sample data as described in the question
lengths <- c(4,5,6,7)
widths <- c(5,6,2,4)
colors <- c("red","blue","green","yellow")

colors[lengths>=5 & widths>=5]           # ANSWER
[1] "blue"
#23e.
# setup some sample data as described in the question
lengths <- c(4,5,6,7)
widths <- c(5,6,2,4)
colors <- c("red","blue","green","yellow")

colors[lengths*widths>=25]           # ANSWER
[1] "blue"   "yellow"
# 23f.

# setup some sample data as described in the question
lengths <- c(4,5,6,7)
widths <- c(5,6,2,4)
colors <- c("red","blue","green","yellow")

lengths[colors=="blue" & lengths*widths>=25]           # ANSWER
[1] 5
# 23g. Show the lengths of the rectangles whose area is at least 25 and whose color
#       is any one of blue, green or yellow.

# setup some sample data as described in the question
lengths <- c(4,5,6,7)
widths <- c(5,6,2,4)
colors <- c("red","blue","green","yellow")

# ONE ANSWER: the parentheses are necessary due to the order of operations of & before |
lengths[lengths*widths>=25 & (colors=="blue" | colors=="green" | colors=="yellow") ]    # ANSWER
[1] 5 7
# ANOTHER ANSWER: this uses the %in% operator
lengths[lengths*widths>=25 & colors %in% c("blue","green","yellow") ]    # ANOTHER ANSWER
[1] 5 7

QUESTION 24 TOPICS: vectors, parallelVectors

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 24
# TOPICS: vectors, parallelVectors
#
# The vector "roomDimensions" is used to store the lengths, widths and
# heights of several rooms in the following format:
# lengthRoom1,widthRoom1,heightRoom1,lengthRoom2,widthRoom2,heightRoom2, etc. 
#
# FOR EXAMPLE, the following code :
#
#         > roomDimensions <- c(10,4,10,11,5,9,12,5,10.5,8,5,9.5)
#
# represents information about four different rooms with the following dimensions. 
#
#          first room:  length is 10, width is 4, height is 10
#          second room: length is 11, width is 5  height is 9
#          third room: length is 12, width is 5, height is 10.5
#          fourth room: length is 8,  width is 5, height is 9.5
#
# Different numbers may be assigned to roomDimensions, but the structure is as 
# described above. 
#
#    ANSWER PARTS a,b,c,d,e SHOWN BELOW
#      The answer to (a) should display:   4
#      The answer to (b) should display:   11  5  9
#      The answer to (c) should display:   40  55  60  40
#      The answer to (d) should display:   2  3
#      The answer to (e) should display:   9  10
# 
#      NOTE that your code for (a),(b),(c),(d) and (e)  should work no
#      matter what values are placed in the variable, roomDimensions
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

QUESTION 24a (see the intro to question 24 above) # TOPICS: vectors, length

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 24a (see the intro to question 24 above)
# TOPICS: vectors, length
#
# Write R code to display how many "rooms" are represented in the vector,
# i.e. 1/3 of the number of values in the vector.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# set the roomDimensions vector to some sample data as described in the question
#
#   The first  room is 10ft long, 4ft wide, 10ft tall (ie. the first three numbers in the vector)
#   The second room is 11ft long, 5ft wide, 12ft tall (ie. the next three numbers in the vector)
#   The third  room is 10ft long, 4ft wide, 10ft tall (ie. the next three numbers in the vector)
#   The fourth room is 11ft long, 5ft wide, 12ft tall (ie. the next three numbers in the vector)
roomDimensions <- c(10,4,10,11,5,9,12,5,10.5,8,5,9.5)

# The number of rooms is 1/3 the number of numbers (since each room is described with 3  numbers)
length(roomDimensions) / 3   # ANSWER
[1] 4

QUESTION 24b (see the intro to question 24 above) TOPICS: vectors

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 24b (see the intro to question 24 above)
# TOPICS: vectors
#
#    Write R code to display the length, width and height of the nth room 
#    where n is a variable that contains a number. For example, if "n" contains 
#    the number 4 then your code should display the 3 numbers that represent the 
#    length, width and height of the 4th room.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# setup some sample data (see the question above)
roomDimensions <- c(10,  4,  10,  11,  5,  9,  12,  5,  10.5,  8,  5,  9.5)

n <- 4    # This is the number of the room we want (i.e. 1 for 1st room, 2 for 2nd room, etc)

# Display the dimensions for just the nth room
#
# To explain the following answer, it helps to think of a particular example.
# Suppose we were trying to get the dimensions for the 4th room ...
# ... keep in mind, that the first number for the 4th room appears right after the last number for the 3rd room.
# ... since there are 3 numbers for each room there are 9 numbers before the first number for the 4th room.
# ... i.e. there are 3*3 = 9 numbers for the first 3 rooms so the 4th room starts at position 10.
# ... to generalize, the first number for the nth room starts at (n-1)*3+1. In the example I just mentioned
# ... n is 4, so (n-1) is 3, so (n-1)*3 is 9 so the first number for the 4th room starts at position
# ... (n-1)*3+1 which is position 10.


roomDimensions[c(10,11,12)]
[1] 8.0 5.0 9.5
n=4
roomDimensions[c(10,11,n*3)]
[1] 8.0 5.0 9.5
roomDimensions[ c( (n-1)*3+1 , (n-1)*3+2 , (n-1)*3+3 ) ] # ANSWER
[1] 8.0 5.0 9.5
# Another way to think of this problem is that the last number for the 4th room is
# at position 4*3 i.e. the 12th number. Since each room as 3 numbers, the first number
# for the 4th room is at position 4*3-1 and the second number for the 4th room is at position 4*3-2.
# Therefore the 3 numbers for the 4th room are at positions 4*3-2 (i.e. 10) , 4*3-1 (i.e. 11) and 4*3 (i.e. 12)
# To generalize, the 3 numbers for the nth room are at positions n*3-2 , n*3-1 and n*3 

roomDimensions[ c(n*3-2, n*3-1, n*3) ]                   # ANOTHER ANSWER
[1] 8.0 5.0 9.5

QUESTION 24c (see the intro to question 24 above) # TOPICS: vectors, parallelVectors

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 24c (see the intro to question 24 above)
# TOPICS: vectors, parallelVectors
#
# Write R code to display the number of square feet of floor space in each room.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# setup some sample data (see the question above)
roomDimensions <- c(10,4,10,11,5,9,12,5,10.5,8,5,9.5)        

# Get a vector with just the lengths of each room (i.e. 10,11,12,8)
lengths <- roomDimensions[ seq(1,length(roomDimensions),by=3) ] # ANSWER (MULTIPLE LINES)   10 11 12 8
lengths
[1] 10 11 12  8
# Get a vector with just the widths of each room (i.e. 4,5,5,5)
widths <- roomDimensions[ seq(2,length(roomDimensions),by=3)  ] # ANSWER (MULTIPLE LINES) 4 5 5 5
widths
[1] 4 5 5 5
# multiply the lengths and widths to get the square feet of floor space
lengths * widths                                                # ANSWER (MULTIPLE LINES) 40 55 60 40
[1] 40 55 60 40

QUESTION 24d (see the intro to question 24 above) TOPICS: vectors, parallelVectors

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 24d (see the intro to question 24 above)
# TOPICS: vectors, parallelVectors
#
# Write R code to display the room numbers of those rooms that have 
# at least 50 square feet of floor space.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# setup some sample data (see the question)
roomDimensions <- c(10,4,10,11,5,9,12,5,10.5,8,5,9.5)        

# extract just the lengths into a new vector (10 11 9 10.5)
lengths <- roomDimensions[ seq(1,length(roomDimensions),by=3) ] # ANSWER (MULTIPLE LINES) 10 11 9 10.5
lengths
[1] 10 11 12  8
# extract just the widths into a new vector (4 5 5 5)
widths <- roomDimensions[ seq(2,length(roomDimensions),by=3) ]  # ANSWER (MULTIPLE LINES) 4 5 5 5
widths
[1] 4 5 5 5
# create a vector with the room numbers, i.e. 1 2 3 4
roomNumbers <- 1:(length(roomDimensions)/3)                     # ANSWER (MULTIPLE LINES) 1 2 3 4
roomNumbers
[1] 1 2 3 4
# display the room numbers for those rooms whose length*width is at least 50
roomNumbers[lengths * widths >= 50]                             # ANSWER (MULTIPLE LINES)  2 3
[1] 2 3

QUESTION 24e (see the intro to question 24 above) TOPICS: vectors, parallelVectors

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 24e (see the intro to question 24 above)
# TOPICS: vectors, parallelVectors
#
# Write R code to display the heights of those rooms that have 
# at least 50 square feet of floor space.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
######################.
# ONE POSSIBLE ANSWER
######################.
# setup some sample data (see the question)
roomDimensions <- c(10,4,10,11,5,9,12,5,10.5,8,5,9.5)        

# extract just the lengths into a new vector (10 11 9 10.5)
lengths <- roomDimensions[ seq(1,length(roomDimensions),by=3) ] # ANSWER (MULTIPLE LINES) 10 11 9 10.5
lengths
[1] 10 11 12  8
# extract just the widths into a new vector (4 5 5 5)
widths <- roomDimensions[ seq(2,length(roomDimensions),by=3) ]  # ANSWER (MULTIPLE LINES) 4 5 5 5
widths
[1] 4 5 5 5
# extract just the heights into a new vector (10 9 10.5 9.5)
heights <- roomDimensions[ seq(3,length(roomDimensions),by=3) ] # ANSWER (MULTIPLE LINES)
heights
[1] 10.0  9.0 10.5  9.5
# get the heights for just the rooms whose area is at least 50 (final answer)
heights[lengths * widths >= 50]                                # FINAL ANSWER (MULTIPLE LINES)  9 10
[1]  9.0 10.5
###########################.
# ANOTHER POSSIBLE ANSWER
###########################.

# setup some sample data (see the question)
roomDimensions <- c(10,4,10,11,5,9,12,5,10.5,8,5,9.5)        

# extract just the lengths into a new vector (10 11 9 10.5)
lengths <- roomDimensions[ seq(1,length(roomDimensions),by=3) ] # ANSWER (MULTIPLE LINES) 10 11 9 10.5
lengths
[1] 10 11 12  8
# extract just the widths into a new vector (4 5 5 5)
widths <- roomDimensions[ seq(2,length(roomDimensions),by=3) ]  # ANSWER (MULTIPLE LINES) 4 5 5 5
widths
[1] 4 5 5 5
# extract just the heights into a new vector (10 9 10.5 9.5)
heights <- roomDimensions[ seq(3,length(roomDimensions),by=3) ] # ANSWER (MULTIPLE LINES)
heights
[1] 10.0  9.0 10.5  9.5
# create a vector with the room numbers, i.e. 1 2 3 4
roomNumbers <- 1:(length(roomDimensions)/3)                     # ANSWER (MULTIPLE LINES) 1 2 3 4 
roomNumbers
[1] 1 2 3 4
# find the room numbers for the rooms that have at least 50sq feet of floor space 
desiredRooms <- roomNumbers[lengths * widths >= 50]             # ANSWER (MULTIPLE LINES)  2 3 
desiredRooms
[1] 2 3
# get the heights for just those rooms (final answer)
heights[desiredRooms]                                           # FINAL ANSWER (MULTIPLE LINES)  9 10
[1]  9.0 10.5

QUESTION 25 # TOPICS: vectors

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 25
# TOPICS: vectors
#
# Display the sum of the squares of the numbers in the vector nums.
# For example, if nums contains the values 4 2 and 5 then your command should
# display the number 45, (i.e. 16 + 4 + 25)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nums <- c(4,2,5)
sum(nums^2)        # ANSWER
[1] 45

QUESTION 26 TOPICS: vectors, indexingWithLogicalValues, vectorAssignment

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 26
# TOPICS: vectors, indexingWithLogicalValues, vectorAssignment
#
# Grades is a vector that contains grades on a test. 
# Modify the vector, grades, by adding 5 points to all grades that are below 60.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
grades <- c(53,70,49,60,90)
grades[grades < 60] <- grades[grades < 60] + 5   # ANSWER
grades
[1] 58 70 54 60 90

QUESTION 27 TOPICS: vectors, indexingWithNumbers

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 27
# TOPICS: vectors, indexingWithNumbers
#
#  Reverse the contents of the vector x. DO NOT USE THE rev FUNCTION. 
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
x <- seq(10,50,by=10)
x
[1] 10 20 30 40 50
x <- x[length(x):1]     # ANSWER
x
[1] 50 40 30 20 10

QUESTION 28 TOPICS: vectors, mean, length, indexingWithLogicalValues

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 28
# TOPICS: vectors, mean, length, indexingWithLogicalValues
#
# grades is a vector that contains grades on a test. Display the number of students 
# whose grade is equal to the average grade. For example, if grades contains
# c(80,85,85,85,90) then the answer should be 3 since the average is 85 and 3 students
# received exactly and 85. 
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
grades <- c(80,85,85,85,90)
mean(grades)
[1] 85
length( grades[grades == mean(grades)] )  # ANSWER
[1] 3

QUESTION 29 TOPICS: vectors, seqOrColon, minMax

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 29
# TOPICS: vectors, seqOrColon, minMax
#
# store in the vector nums a sequence of values from the smallest value in the
# vector x to the largest value in x. For example if x would contain c(7,8,7,3,5),
# your code should set the value of nums to the sequence 3 4 5 6 7 8
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
x <- c(7,10,7,3,5)
min(x)
[1] 3
max(x)
[1] 10
nums <- min(x):max(x)   # ANSWER
nums
[1]  3  4  5  6  7  8  9 10

QUESTION 30 TOPICS: vectors, minMax, c

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 30
# TOPICS: vectors, minMax, c
#
# Write a single command that shows the lowest and the highest values from the
# vector x. For example if x contains c(20,-5,-2,40,25,-2,18), then your answer
# should display -5 40
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
x <- c(20,-5,-2,40,25,-2,18)    # start with some data for x
c(min(x),max(x))                # ANSWER
[1] -5 40

QUESTION 31 TOPICS: vectors, roundTruncCeilingFloor, sqrt, ==and!= , %/%and%%

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 31
# TOPICS: vectors, roundTruncCeilingFloor, sqrt, ==and!= , %/%and%%
#
# Determine if the number stored in n is a "perfect square". For example 25 is
# a "perfect square" because 5*5 is 25. Similarly, 49 is a perfect square because
# 7*7 is 49. However, 50 is NOT a perfect square since its square root is not an integer.
# Your code should display the value TRUE is n is a perfect square and FALSE if n is
# not a perfect square. Hint: the sqrt(x) function returns the square root of x.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
n <- 25                   # let's try with a number that IS a perfect square
trunc(sqrt(n)) == sqrt(n) # ANSWER    (TRUE)
[1] TRUE
floor(sqrt(n)) == sqrt(n) # ANOTHER POSSIBLE ANSWER   (TRUE)
[1] TRUE
n%%sqrt(n) == 0           # YET ANOTHER POSSIBLE ANSWER   (TRUE)
[1] TRUE
# ------- CHECK -------
n <- 30                   # now, just to make sure, let's try with a number that is NOT a perfect square
sqrt(n)
[1] 5.477226
trunc(sqrt(n))
[1] 5
trunc(sqrt(n)) == sqrt(n) # ANSWER   (FALSE)
[1] FALSE
floor(sqrt(n)) == sqrt(n) # ALTERNATE ANSWER   (FALSE)
[1] FALSE
n%%sqrt(n) == 0           # YET ANOTHER POSSIBLE ANSWER   (FALSE)
[1] FALSE

QUESTION 32 TOPICS: vectors, roundTruncCeilingFloor, &|, ==!=

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 32
# TOPICS: vectors, roundTruncCeilingFloor, &_|, ==_!=
#
# Write a command that shows the numbers in the vector nums that are both even
# and perfect squares. For example, if nums contains c(81, 6, 36, 10, 4, 25)
# then your answer should display 36  4
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
x[sqrt(x)^2 == floor(x)  &  x%%2 == 0]    # ANSWER
Warning in sqrt(x): NaNs produced
[1] NA NA

QUESTION 33 TOPICS: vectors

############################################################################################################.
# NOTE: The sort(x) function will return a vector with the values from the vector x in sorted order. 
#       The unique(x) function will return a copy of the vector x with all of the duplicate values removed. 
#       These functions will help you to answer the following questions
############################################################################################################.

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 33
# TOPICS: vectors
#
# Write a command that displays the 2nd lowest value in the vector x. If the lowest value appears
# more than once in the vector, then your code should display that value. For example, if x contains
# the numbers 7 3 3 7 3 5 20, the function should return the number 3. Even though 3 is the lowest
# value, it appears more than once, so in that sense, it is the 2nd lowest value.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
x <- c(7,3,3,7,3,5,20)
sort(x)
[1]  3  3  3  5  7  7 20
sort(x)[2]            # ANSWER
[1] 3
# The following is just another example. It is NOT a new answer. 
x <- c(7,7,3,5,20) # ANOTHER EXAMPLE
sort(x)            # 
[1]  3  5  7  7 20
sort(x)[2]         # 
[1] 5

QUESTION 34 TOPICS: vectors

############################################################################################################.
# NOTE: The sort(x) function will return a vector with the values from the vector x in sorted order. 
#       The unique(x) function will return a copy of the vector x with all of the duplicate values removed. 
#       These functions will help you to answer the following questions
############################################################################################################.

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 34
# TOPICS: vectors
#
# Write a command that displays the 2nd lowest value in the vector x. For this question, you
# should ignore duplicate values. For example, if x contains the numbers 7 3 3 7 3 5 20, the function
# should return the number 5. Even though 3 appears more than once, 5 is the 2nd lowest value.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ONE ANSWER
x <- c(7,3,3,7,3,5,20)
unique(x)
[1]  7  3  5 20
sort(x)
[1]  3  3  3  5  7  7 20
sort(unique(x))
[1]  3  5  7 20
sort(unique(x))[2]            # ANSWER
[1] 5
# ANOTHER ANSWER
x <- c(7,3,3,7,3,5,20)
xWithoutLowestNumber = x[ x != min(x) ]  # ANSWER MULTIPLE LINES
min(xWithoutLowestNumber)                # ANSWER MULTIPLE LINES
[1] 5

QUESTION 35 TOPICS: vectors

############################################################################################################.
# NOTE: The sort(x) function will return a vector with the values from the vector x in sorted order. 
#       The unique(x) function will return a copy of the vector x with all of the duplicate values removed. 
#       These functions will help you to answer the following questions
############################################################################################################.

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 35
# TOPICS: vectors
#
# Write a command that displays the n lowest values in the vector x where n is
# a variable that contains a positive integer. The values should be displayed
# in increasing order.
#
# For example if x contains c(67,2,40,5,2,99) and n contains the number 3 then
# the command should display the numbers 2 2 and 5
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
n <- 3
x <- c(67,2,40,5,2,99)
x
[1] 67  2 40  5  2 99
sort(x)
[1]  2  2  5 40 67 99
sort(x)[1:n]            # ANSWER
[1] 2 2 5

QUESTION 36 TOPICS: vectors

############################################################################################################.
# NOTE: The sort(x) function will return a vector with the values from the vector x in sorted order. 
#       The unique(x) function will return a copy of the vector x with all of the duplicate values removed. 
#       These functions will help you to answer the following questions
############################################################################################################.

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 36
# TOPICS: vectors
#
# Same as previous question, but the numbers should be in decreasing order. 
# For example, given the values from the previous question, the answer should
# display 5 2 2. Hint, see the help for the sort command and focus on the
# "decreasing" argument to sort.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
n <- 3
x <- c(67,2,40,5,2,99)
x
[1] 67  2 40  5  2 99
sort(x)
[1]  2  2  5 40 67 99
sort(x)[1:n]
[1] 2 2 5
sort(sort(x)[1:n], decreasing=TRUE)   # ANSWER
[1] 5 2 2
# ALTERNATE ANSWER

n <- 3
x <- c(67,2,40,5,2,99)
x
[1] 67  2 40  5  2 99
x <- sort(x, decreasing=TRUE)  # ANOTHER ANSWER (MULTIPLE LINES)
x
[1] 99 67 40  5  2  2
(length(x)-n+1)
[1] 4
(length(x)-n+1):length(x)
[1] 4 5 6
x[(length(x)-n+1):length(x)]   # ANOTHER ANSWER (MULTIPLE LINES)
[1] 5 2 2

QUESTION 37 TOPICS: vectors

############################################################################################################.
# NOTE: The sort(x) function will return a vector with the values from the vector x in sorted order. 
#       The unique(x) function will return a copy of the vector x with all of the duplicate values removed. 
#       These functions will help you to answer the following questions
############################################################################################################.

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 37
# TOPICS: vectors
#
# Same as previous question, but the numbers should be displayed in the same order
# as they appear in the original vector x. Given the same values from the previous
# question, the answer should display the numbers  2  5  2  in that order. 
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
n <- 3
x <- c(67,2,40,5,2,99)
sort(x)[n]
[1] 5
x[x <= sort(x)[n]]     # ANSWER
[1] 2 5 2

QUESTION 38 TOPICS: vectors

############################################################################################.
# QUESTION_GROUP:
#
# (The next two questions are related to each other. I would have numbered them a,b,etc but 
#  I thought it would be better for each question to have its own number.)
##########################################################################################.

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 38
# TOPICS: vectors
#
# x and y each contain a single integer. nums is a vector that contains many numbers. 
# You are told that the value of x is guaranteed to be less than the value of y.
# Change the vector nums so that it only contains the values that are in the range
# between x and y (including x and y). For example, if x contains 3, y contains 7 and
# nums contains c(10,3,2,11,6,4,1) then your code should change the value of nums to a vector
# containing 3 6 and 4..
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ONE POSSIBLE ANSWER
# The following answer is done in two different steps
x <- 3 # setup some example data
y <- 7 # setup some example data
nums <- c(10,3,2,11,6,4,1) # setup some example data

nums <- nums[nums>=x]      # ANSWER (MULTIPLE LINES) - nums will now contain all the values that
# are greater than or equal to x. Given the example data above, nums,
# will now contain c(10,3,11,6,4). The 2 and 1 were eliminated from nums.

nums                       # show the new values in nums
[1] 10  3 11  6  4
nums <- nums[nums<=y]      # ANSWER (MULTIPLE LINES) -  nums will now contain only those values 
# that are less than or equal to y. Given the example data above, nums
# will now contain c(3,6,4). The 10, and 11 were eliminated from nums.

nums                       # show the final value of nums
[1] 3 6 4
# ANOTHER POSSIBLE ANSWER  -  this answer is "shorter" and more "to the point". However, this answer
#                             requires the use of the & operator.
x <- 3   # setup some example data
y <- 7   # setup some example data
nums <- c(10,3,2,11,6,4,1)   # setup some example data

nums >= x      # given the example data above, this is a logical vector that contains TRUE for every value
[1]  TRUE  TRUE FALSE  TRUE  TRUE  TRUE FALSE
# of nums that is >= x. Since nums contains c(10,3,2,11,6,4,1) and x contains the number 3,
# the logical vector will be c(TRUE,TRUE,FALSE,TRUE,TRUE,TRUE,FALSE)

nums <= y      # given the example data above, this is a logical vector that contains TRUE for every value
[1] FALSE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE
# of nums that is <= y. Since nums contains c(10,3,2,11,6,4,1) and y contains the number 7,
# the logical vector will be c(FALSE,TRUE,TRUE,FALSE,TRUE,TRUE,TRUE)
nums>=x & nums <= y   # This will combine the two logical vectors shown above using the & operator.
[1] FALSE  TRUE FALSE FALSE  TRUE  TRUE FALSE
# That will result in a vector where there is a TRUE in the positions that had
# TRUE for both nums>=x and for num<=y (see above). In other words, the resulting
# logical vector will contain a TRUE in those positions that correspond to values
# in nums that are BOTH >=x AND <=y.
# 
#    as explained above, "nums >= x" is:    c(TRUE ,TRUE ,FALSE,TRUE ,TRUE ,TRUE ,FALSE)
#    as explained above, "nums <= y" is:    c(FALSE,TRUE ,TRUE ,FALSE,TRUE ,TRUE ,TRUE)
# 
# Therefore, "nums>=x & nums <= y" is:      c(FALSE,TRUE ,FALSE,FALSE,TRUE ,TRUE ,FALSE)

nums <- nums[nums>=x & nums <= y]  # ANSWER - "nums <-" simply means that we are going to replace 
#          the values in "nums" with the result of the code from the 
#          right hand side of the arrow.
#     
#          The code on the "Right Hand Side" (RHS) of the arrow is:
#          "nums[nums>=x & nums<=y]". The first "nums" on the RHS of 
#          the arrow indicates that we will be taking values from "nums".
#          the [brackets] that surround the rest of the code indicate 
#          that the code inside the [brackets] will be used to identify
#          which values from nums we will be "taking". As shown above
#          the code inside the brackets results in a logical vector
#          that contains a TRUE for each value that we want from nums. 
#          
#          Given the example data above, this code therefore becomes the 
#          equivalent of: 
#          nums <- nums[c(FALSE,TRUE ,FALSE,FALSE,TRUE ,TRUE ,FALSE)]
#          
#          This results in replacing nums with the 2nd 5th and 6th values
#          from nums (i.e. those positions that contains TRUE). Therefore
#          since "nums" originally contained c(10,3,2,11,6,4,1), the 
#          new value of "nums" will be c(3,6,4)

QUESTION 39 TOPICS: vectors

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 39
# TOPICS: vectors
#
# Same as previous question, except for this question the value in x may be less than,
# greater than or equal to y. Hint: use the sort() function to isolate the smaller
# of x and y and the larger of x and y. Then proceed similarly to the way you answered
# the previous question. 
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# ONE ANSWER:
#
# To understand the answer to this question, you must first understand the answer to the 
# previous question. Please first review the answer to the previous question before continuing.
#
# The difference between this question and the previous question is that in the previous 
# question, you were told that the value of the variable "x" is a number that is LESS THAN
# the value in the variable "y". For example, in the previous question, it would be possible
# for x to be 3 and y to be 7 or x to b 2 and y to b 5, but it would NOT be possible for x
# to be 7 and y to be 3. HOWEVER, in this version of the question, x could be ANY number
# and y could be ANY number. 
#
# Therefore when you write the code to find the values in "nums" that are between x and y 
# you know that the numbers that you desire are all greater than x and less than y. Therefore
# in the previous question, you knew that nums>=x needed to be TRUE and nums<=y needed to be TRUE.
#
# However, in this question you DON'T know which number is less, x or y. Therefore
# if x is less than y then the code from the previous question would work. For example,
# if x is 3 and y is 7 then the expression   5>=3 & 5<=7   would result in a TRUE value.
#
# However, if y were less then x (e.g. x is 7 and y is 3) then "nums>=x & nums<=y" would
# result in a FALSE value for EVERY number. For example the number 5 is obviously between
# x=7 and y=3. However, the expression    5>=7 & 5<=3 would be FALSE.
#
# One approach to answer this question is given the values in x and y, to have your code
# "figure out" which value is higher and which is lower. You can store the lower value in a
# variable named "lower" and the higher value in a variable named "higher". Then you can simply use "lower"
# value as you used "x" in the previous question and use "higher" as you used "y" in the 
# previous question. This is exactly what is done by the code below …


x <- 7   # setup some example data
y <- 3   # setup some example data
nums <- c(10,3,2,11,6,4,1)   # setup some example data

sort(c(x,y))                # "c(x,y)" is a vector that contains the values of x and y. Given the
[1] 3 7
# example data above, since x is 7 and y is 3, c(x,y) is c(7,3). 
#
# The "sort" function takes a vector and sorts its contents. So
# sort(c(7,3)) results in the vector c(3,7)

lower <- sort(c(x,y))[1]  # As described above, given the example data above, the code:
# "sort(c(x,y))" will result in the vector c(3,7). The "[1]" that
# appears after "sort(c(x,y))" extracts the first value from the 
# sorted vector. Given the data above, the first value of c(3,7) is 3.
# This number, 3, is then assigned to the variable "lower".
lower
[1] 3
higher<- sort(c(x,y))[2]  # In a similar way, this code assigns the 2nd value from the sorted vector
# to the variable "higher". Specifically, given the example data, the value
# 7 will be assigned to the variable "higher".
# 
# Note that if the values in x and y were initially 3 and 7 instead of 7 and 3,
# we would still get the 3 being assigned to lower and 7 being assigned to higher.
# Now that we have variables "lower" and "higher", the answer is exactly
# the same as the answer to the previous question, except that instead of using
# x and y in the answer we will use "lower" and "higher".
higher
[1] 7
nums <- nums[nums>=lower]       # ANSWER (MULTIPLE LINES)
nums
[1] 10  3 11  6  4
nums <- nums[nums<=higher]      # ANSWER (MULTIPLE LINES)
nums
[1] 3 6 4
# ANOTHER POSSIBLE ANSWER  -  this answer is "shorter" and more "to the point". However, this answer
#                             requires the use of the & operator. See the explanation of the answer 
#                             to the previous question for more detail.
x <- 7
y <- 3
lower <- sort(c(x,y))[1]
higher<- sort(c(x,y))[2]
nums <- c(10,3,2,11,6,4,1)
nums <- nums[nums>=lower & nums <= higher]      # ANSWER

QUESTION 40 TOPICS: vectors

############################################################################################.
# QUESTION_GROUP:
#
# The next few questions are related to each other.
# I would have numbered them a,b,etc but I thought it would be better
# for each question to have its own number.
############################################################################################.

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 40
# TOPICS: vectors
#
# nums is a vector that contains several numbers. n contains a single positive integer.
# Write code that "shifts" the values in nums over to the right "n" times. For example,
# if nums contains 10 20 30 40 50 60 70 80 90 100 and n is 2 then your command should
# reset nums to 90 100 10 20 30 40 50 60 70 80. If n were 3, your command should
# reset nums to 80 90 100 10 20 30 40 50 60 70
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# The following breaks up the answer into several lines so that it is easier to understand.
# The general idea of the solution below is to figure out which values from the original vector
# will be moved to the beginning of the vector. 

nums <- seq(10,100,by=10)   # setup some example data
nums
 [1]  10  20  30  40  50  60  70  80  90 100
n <- 3                      # setup some example data
positionOfLastNumber <- length(nums)-n                     #ANSWER (MULTIPLE LINES) - This is the position
#   of the value from the original vector that 
#   will be in the last position of the answer.
#   For example if nums is:
#        c(10,20,30,40,50,60,70,80,90,100)
#   and n is 3 then the final answer will be
#        c(80,90,100, 10,20,30,40,50,60,70)
#   Therefore the 70 which is the last number 
#   in the answer, is in the 7th position in nums.

positionOfLastNumber
[1] 7
nums[positionOfLastNumber]
[1] 70
firstPiece <- nums[ (positionOfLastNumber+1):length(nums)] #ANSWER (MULTIPLE LINES) - this code captures
#   the first few numbers in the answer, which
#   are the last few numbers in nums. These numbers
#   start at the position after the position we 
#   calculated above and extend until the end of
#   the nums vector. 
# 
#   For example if nums is:
#        c(10,20,30,40,50,60,70,80,90,100)
#   and n is 3 then the final answer will be
#        c(80,90,100, 10,20,30,40,50,60,70)
#   Therefore the 70 which is the last number 
#   in the answer, is in the 7th position in nums.
#   However, the 80 (which is in the 7th+1 postion,
#   (i.e. the 8th position) in nums will be in the 
#   first position in the final answer. The rest
#   of the numbers in nums from the 80 through 100
#   will be in the first few positions of the answer.
# 
#   We use the variable "firstPiece" to capture
#   those numbers that will be in the first few 
#   positions in the answer.
firstPiece
[1]  80  90 100
lastPiece <- nums[1:(positionOfLastNumber)]                #ANSWER (MULTIPLE LINES) -  the last few numbers
#   in the answer will start from the first positon
#   in "nums" and extend until the postion that we
#   calculated above. We store those numbers
#   in the variable, "lastPiece".
lastPiece
[1] 10 20 30 40 50 60 70
nums <- c(firstPiece, lastPiece)                           #ANSWER (MULTIPLE LINES) - at this point we have
#   the first few numbers for the answer stored in 
#   the variable "firstPiece" and the last few
#   numbers from the answer stored in the variable
#   "lastPiece". All that remains to be done is 
#   to create a new vector with the numbers in that
#   order and assign the result back to the variable
#   "nums".
nums          
 [1]  80  90 100  10  20  30  40  50  60  70
# Alternatively, you could write the answer in one line
nums <- seq(10,100,by=10)
nums
 [1]  10  20  30  40  50  60  70  80  90 100
n <- 3
nums <- c(nums[ (length(nums)-n+1):length(nums)] , nums[1:(length(nums)-n)] )   # ALTERNATIVE ANSWER
nums          
 [1]  80  90 100  10  20  30  40  50  60  70

QUESTION 41 TOPICS: vectors

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 41
# TOPICS: vectors
#
# Same as previous question, but the numbers at the end of nums should be
# removed and the beginning of nums should be filled in with "n" zeros. For example,
# if nums contains 10 20 30 40 50 60 70 80 90 100 and n is 2 then your command should
# reset nums to 0 0 10 20 30 40 50 60 70 80. If n were 3, your command should
# reset nums to 0 0 0 10 20 30 40 50 60 70
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nums <- seq(10,100,by=10)
nums
 [1]  10  20  30  40  50  60  70  80  90 100
n <- 3
nums <- c(rep(0,n) , nums[1:(length(nums)-n)] )  # ANSWER
nums          
 [1]  0  0  0 10 20 30 40 50 60 70

QUESTION 42 TOPICS: vectors

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 42
# TOPICS: vectors
#
# Same as above, but replace the numbers at the beginning of nums with 1,2,3,etc.
# For example, if nums contains 10 20 30 40 50 60 70 80 90 100 and n is 2 then your
# command should reset nums to 1 2 10 20 30 40 50 60 70 80. If n were 3, your command
# should reset nums to 1 2 3 10 20 30 40 50 60 70
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nums <- seq(10,100,by=10)
nums
 [1]  10  20  30  40  50  60  70  80  90 100
n <- 3
nums <- c(1:n , nums[1:(length(nums)-n)] )   # ANSWER
nums          
 [1]  1  2  3 10 20 30 40 50 60 70

QUESTION 43 TOPICS: vectors

############################################################################################.
# QUESTION_GROUP: 
# (The next few questions are related to each other. I would have numbered them a,b,etc but 
#  I thought it would be better for each question to have its own number.)
############################################################################################.

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 43
# TOPICS: vectors
#
# The vector nums contains several numbers. Display the values of nums without the
# smallest numbers. For example, if nums contains c(30,5,20,5,30,15) then your command
# should display 30 20 30 15(none of the 5's are displayed since 5 is the smallest number).
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nums <- c(30,5,20,5,30,15) 
min(nums)
[1] 5
nums != min(nums)
[1]  TRUE FALSE  TRUE FALSE  TRUE  TRUE
nums[nums != min(nums)]    # ANSWER
[1] 30 20 30 15
nums[nums > min(nums)]     # ALTERNATIVE ANSWER
[1] 30 20 30 15

QUESTION 44 TOPICS: vectors

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 44
# TOPICS: vectors
#
# Same as the previous question, but do not display any of the smallest or the
# largest numbers. For example, if nums contains c(30,5,20,5,30,15) then your command
# should display 20 15 (none of the 5's are displayed since 5 is the smallest number and
# none of the 30's are displayed since 30 is the largest number).
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

##############################################################################################.
# END_OF_QUESTION_GROUP
##############################################################################################.
# The best way to do this is with the "&" operator. If you haven't learned the "&" operator yet
# There are alternatives below
nums <- c(30,5,20,5,30,15) 
nums[nums != min(nums) & nums != max(nums)]   # ANSWER
[1] 20 15
nums[nums > min(nums) & nums < max(nums)]     # ALTERNATIVE ANSWER
[1] 20 15
# This version of the answer does NOT use the & operator, but requires two lines of code.
# These alternatives use the != (i.e. "not equal to") operator
nums <- c(30,5,20,5,30,15) 
temp <- nums[nums != min(nums)]  #ANSWER (MULTIPLE LINES)
temp[temp != max(nums)]          #ANSWER (MULTIPLE LINES)
[1] 20 15
# This version of the answer does NOT use the & operator, but requires two lines of code.
# These alternatives use the > and < operators instead of the != operator
nums <- c(30,5,20,5,30,15) 
temp <- nums[nums > min(nums)]  #ALTERNATIVE ANSWER (MULTIPLE LINES)
temp[temp < max(nums)]          #ALTERNATIVE ANSWER (MULTIPLE LINES)
[1] 20 15

QUESTION 45 TOPICS: vectors

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# QUESTION 45
# TOPICS: vectors
#
# x and y are two vectors whose lengths are the same. Merge the contents of
# vector y into vector x in the following way. If x1,x2,x3,x4,... represent the
# initial values in x and y1,y2,y3,y4,... represent the values in y, then after
# your commands run, the values in x should be x1,y1,x2,y2,x3,y3,x4,y4,...
#
# For example, if x contains the values 1 2 3 4 and y contains the values 11 22 33 44 
# then after your code runs the values in x should be 1 11 2 22 3 33 4 44. 
# (You may use more than one command to accomplish this result).
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
x <- c(1,2,3,4)
x
[1] 1 2 3 4
y <- c(11,22,33,44)
y
[1] 11 22 33 44
seq(1,2*length(x),by=2)
[1] 1 3 5 7
x[seq(1,2*length(x),by=2)] <- x    # ANSWER (MULTIPLE LINES)

x
[1]  1  2  2  4  3 NA  4
seq(2,2*length(y),by=2)
[1] 2 4 6 8
x[seq(2,2*length(y),by=2)] <- y    # ANSWER (MULTIPLE LINES)
x
[1]  1 11  2 22  3 33  4 44