Solutions

1

  • Create a vector containing all the dates in 2007, using seq and as.Date.
In [24]:
myDate1 = as.Date('1-Jan-2007', format="%d-%b-%Y")
myDate2 = myDate1+364
myDate2
In [25]:
dates2007 = seq(myDate1,myDate2,1)
  • There is a version of cut for dates, called cut.Date. Use this to create a factor with values corresponding to the date of the first day of the week in which each of these dates falls. Start the weeks on Sundays.
In [26]:
fdate = cut.Date(dates2007, "weeks",start.on.monday=FALSE)
table(fdate)
fdate
2006-12-31 2007-01-07 2007-01-14 2007-01-21 2007-01-28 2007-02-04 2007-02-11 
         6          7          7          7          7          7          7 
2007-02-18 2007-02-25 2007-03-04 2007-03-11 2007-03-18 2007-03-25 2007-04-01 
         7          7          7          7          7          7          7 
2007-04-08 2007-04-15 2007-04-22 2007-04-29 2007-05-06 2007-05-13 2007-05-20 
         7          7          7          7          7          7          7 
2007-05-27 2007-06-03 2007-06-10 2007-06-17 2007-06-24 2007-07-01 2007-07-08 
         7          7          7          7          7          7          7 
2007-07-15 2007-07-22 2007-07-29 2007-08-05 2007-08-12 2007-08-19 2007-08-26 
         7          7          7          7          7          7          7 
2007-09-02 2007-09-09 2007-09-16 2007-09-23 2007-09-30 2007-10-07 2007-10-14 
         7          7          7          7          7          7          7 
2007-10-21 2007-10-28 2007-11-04 2007-11-11 2007-11-18 2007-11-25 2007-12-02 
         7          7          7          7          7          7          7 
2007-12-09 2007-12-16 2007-12-23 2007-12-30 
         7          7          7          2 
  • Create x, a vector of length 100, with integer values in the range $1:5$, randomly ordered. (Hint: look at the function sample.)
In [27]:
sample(1:5,100,replace=TRUE)
  1. 5
  2. 3
  3. 4
  4. 3
  5. 2
  6. 3
  7. 2
  8. 2
  9. 5
  10. 2
  11. 5
  12. 1
  13. 3
  14. 3
  15. 2
  16. 2
  17. 1
  18. 3
  19. 5
  20. 1
  21. 5
  22. 3
  23. 4
  24. 5
  25. 1
  26. 1
  27. 1
  28. 4
  29. 4
  30. 4
  31. 1
  32. 5
  33. 4
  34. 3
  35. 1
  36. 2
  37. 5
  38. 4
  39. 5
  40. 3
  41. 1
  42. 4
  43. 5
  44. 2
  45. 5
  46. 1
  47. 5
  48. 5
  49. 2
  50. 5
  51. 2
  52. 4
  53. 4
  54. 1
  55. 3
  56. 4
  57. 1
  58. 2
  59. 1
  60. 5
  61. 3
  62. 5
  63. 2
  64. 4
  65. 2
  66. 2
  67. 1
  68. 1
  69. 4
  70. 1
  71. 4
  72. 5
  73. 1
  74. 1
  75. 4
  76. 5
  77. 3
  78. 4
  79. 3
  80. 3
  81. 5
  82. 1
  83. 1
  84. 3
  85. 2
  86. 1
  87. 2
  88. 1
  89. 3
  90. 2
  91. 1
  92. 4
  93. 3
  94. 1
  95. 2
  96. 5
  97. 5
  98. 5
  99. 5
  100. 3
  • Use paste to create a vector of labels: ("Colour 1", "Colour 2", "Colour 3", "Colour 4", "Colour 5")
In [28]:
x = paste("Colour",1:5)
x
  1. 'Colour 1'
  2. 'Colour 2'
  3. 'Colour 3'
  4. 'Colour 4'
  5. 'Colour 5'
  • Use the factor command to create a factor from the vector x, with the labels created above.
In [29]:
xf = factor(x)
table(xf)
xf
Colour 1 Colour 2 Colour 3 Colour 4 Colour 5 
       1        1        1        1        1 
  • Create a data frame with 100 rows and two columns, one containing a random sample of the vector of dates created above, and the other containing the factor vector of colour names.
In [30]:
(datf = data.frame(d=sample(dates2007,100,replace=TRUE),c=xf)  )
dc
2007-11-10Colour 1
2007-03-03Colour 2
2007-10-24Colour 3
2007-11-10Colour 4
2007-08-25Colour 5
2007-04-27Colour 1
2007-03-03Colour 2
2007-04-06Colour 3
2007-05-13Colour 4
2007-01-14Colour 5
2007-09-25Colour 1
2007-05-20Colour 2
2007-07-19Colour 3
2007-11-08Colour 4
2007-11-03Colour 5
2007-06-04Colour 1
2007-07-04Colour 2
2007-07-03Colour 3
2007-08-04Colour 4
2007-08-20Colour 5
2007-11-20Colour 1
2007-08-16Colour 2
2007-01-25Colour 3
2007-04-07Colour 4
2007-12-19Colour 5
2007-12-25Colour 1
2007-11-29Colour 2
2007-06-19Colour 3
2007-11-21Colour 4
2007-01-25Colour 5
......
2007-12-02Colour 1
2007-03-28Colour 2
2007-09-15Colour 3
2007-04-06Colour 4
2007-09-10Colour 5
2007-02-23Colour 1
2007-11-14Colour 2
2007-12-31Colour 3
2007-06-14Colour 4
2007-05-06Colour 5
2007-04-15Colour 1
2007-02-14Colour 2
2007-11-26Colour 3
2007-08-05Colour 4
2007-08-25Colour 5
2007-07-05Colour 1
2007-07-31Colour 2
2007-09-21Colour 3
2007-09-10Colour 4
2007-03-05Colour 5
2007-12-11Colour 1
2007-12-14Colour 2
2007-11-02Colour 3
2007-10-25Colour 4
2007-03-05Colour 5
2007-02-16Colour 1
2007-01-13Colour 2
2007-07-24Colour 3
2007-02-27Colour 4
2007-05-14Colour 5
  • Select the rows for which the date is after 1st June 2007.
In [31]:
datf[datf$d-as.Date('1-June-2007', format="%d-%b-%Y")>0,]
dc
12007-11-10Colour 1
32007-10-24Colour 3
42007-11-10Colour 4
52007-08-25Colour 5
112007-09-25Colour 1
132007-07-19Colour 3
142007-11-08Colour 4
152007-11-03Colour 5
162007-06-04Colour 1
172007-07-04Colour 2
182007-07-03Colour 3
192007-08-04Colour 4
202007-08-20Colour 5
212007-11-20Colour 1
222007-08-16Colour 2
252007-12-19Colour 5
262007-12-25Colour 1
272007-11-29Colour 2
282007-06-19Colour 3
292007-11-21Colour 4
312007-11-07Colour 1
332007-12-14Colour 3
342007-06-23Colour 4
352007-08-16Colour 5
362007-11-24Colour 1
382007-06-29Colour 3
412007-07-30Colour 1
472007-06-29Colour 2
482007-07-23Colour 3
492007-10-12Colour 4
.........
512007-10-29Colour 1
522007-07-05Colour 2
532007-09-14Colour 3
562007-10-13Colour 1
572007-09-18Colour 2
582007-11-12Colour 3
592007-11-26Colour 4
632007-07-05Colour 3
642007-10-09Colour 4
652007-11-20Colour 5
662007-08-01Colour 1
672007-12-16Colour 2
712007-12-02Colour 1
732007-09-15Colour 3
752007-09-10Colour 5
772007-11-14Colour 2
782007-12-31Colour 3
792007-06-14Colour 4
832007-11-26Colour 3
842007-08-05Colour 4
852007-08-25Colour 5
862007-07-05Colour 1
872007-07-31Colour 2
882007-09-21Colour 3
892007-09-10Colour 4
912007-12-11Colour 1
922007-12-14Colour 2
932007-11-02Colour 3
942007-10-25Colour 4
982007-07-24Colour 3

2

  • Generate a matrix with 10 rows and 5 columns, with random entries between 0 and 10. (Hint: look at runif)
In [32]:
matti = matrix(runif(50,0,10),nrow=10)
matti
9.804293 4.9836626 0.7089561 0.640357358.655358
1.535470 6.3303802 9.0285600 9.584366892.117108
7.054454 0.2246136 6.6611312 6.703343407.309426
4.752587 6.0213062 7.7713002 0.071173143.526904
3.143838 5.7364747 0.4074274 4.433951587.922231
3.176729 1.3561584 2.1107383 2.284208726.819195
5.302103 9.8258713 9.2962555 4.647849423.751291
5.985252 2.3348505 7.1433309 0.542256926.742854
7.494877 1.6589038 0.6764284 9.186249239.676256
1.905350 7.5088342 9.7672090 9.564313395.387983
  • Write a function using for to calculate the column means of the matrix.
In [33]:
for (i in 1:5) mean(matti[,i])
apply(matti,2,mean)
  1. 5.01549536734819
  2. 4.598105562618
  3. 5.35713369795121
  4. 4.76580700511113
  5. 6.1908606744837
  • Extract the even rows from the matrix.
In [34]:
matti[seq(2,10,2),]
1.535470 6.330380 9.028560 9.584366892.117108
4.752587 6.021306 7.771300 0.071173143.526904
3.176729 1.356158 2.110738 2.284208726.819195
5.985252 2.334851 7.143331 0.542256926.742854
1.905350 7.508834 9.767209 9.564313395.387983