seq
and as.Date
.myDate1 = as.Date('1-Jan-2007', format="%d-%b-%Y")
myDate2 = myDate1+364
myDate2
dates2007 = seq(myDate1,myDate2,1)
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.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
x
, a vector of length 100, with integer values in the range
$1:5$, randomly ordered. (Hint: look at the function sample
.)sample(1:5,100,replace=TRUE)
paste
to create a vector of labels:
("Colour 1", "Colour 2", "Colour 3", "Colour 4", "Colour 5")x = paste("Colour",1:5)
x
factor
command to create a factor from the vector x
,
with the labels created above.xf = factor(x)
table(xf)
xf Colour 1 Colour 2 Colour 3 Colour 4 Colour 5 1 1 1 1 1
(datf = data.frame(d=sample(dates2007,100,replace=TRUE),c=xf) )
d | c |
---|---|
2007-11-10 | Colour 1 |
2007-03-03 | Colour 2 |
2007-10-24 | Colour 3 |
2007-11-10 | Colour 4 |
2007-08-25 | Colour 5 |
2007-04-27 | Colour 1 |
2007-03-03 | Colour 2 |
2007-04-06 | Colour 3 |
2007-05-13 | Colour 4 |
2007-01-14 | Colour 5 |
2007-09-25 | Colour 1 |
2007-05-20 | Colour 2 |
2007-07-19 | Colour 3 |
2007-11-08 | Colour 4 |
2007-11-03 | Colour 5 |
2007-06-04 | Colour 1 |
2007-07-04 | Colour 2 |
2007-07-03 | Colour 3 |
2007-08-04 | Colour 4 |
2007-08-20 | Colour 5 |
2007-11-20 | Colour 1 |
2007-08-16 | Colour 2 |
2007-01-25 | Colour 3 |
2007-04-07 | Colour 4 |
2007-12-19 | Colour 5 |
2007-12-25 | Colour 1 |
2007-11-29 | Colour 2 |
2007-06-19 | Colour 3 |
2007-11-21 | Colour 4 |
2007-01-25 | Colour 5 |
... | ... |
2007-12-02 | Colour 1 |
2007-03-28 | Colour 2 |
2007-09-15 | Colour 3 |
2007-04-06 | Colour 4 |
2007-09-10 | Colour 5 |
2007-02-23 | Colour 1 |
2007-11-14 | Colour 2 |
2007-12-31 | Colour 3 |
2007-06-14 | Colour 4 |
2007-05-06 | Colour 5 |
2007-04-15 | Colour 1 |
2007-02-14 | Colour 2 |
2007-11-26 | Colour 3 |
2007-08-05 | Colour 4 |
2007-08-25 | Colour 5 |
2007-07-05 | Colour 1 |
2007-07-31 | Colour 2 |
2007-09-21 | Colour 3 |
2007-09-10 | Colour 4 |
2007-03-05 | Colour 5 |
2007-12-11 | Colour 1 |
2007-12-14 | Colour 2 |
2007-11-02 | Colour 3 |
2007-10-25 | Colour 4 |
2007-03-05 | Colour 5 |
2007-02-16 | Colour 1 |
2007-01-13 | Colour 2 |
2007-07-24 | Colour 3 |
2007-02-27 | Colour 4 |
2007-05-14 | Colour 5 |
datf[datf$d-as.Date('1-June-2007', format="%d-%b-%Y")>0,]
d | c | |
---|---|---|
1 | 2007-11-10 | Colour 1 |
3 | 2007-10-24 | Colour 3 |
4 | 2007-11-10 | Colour 4 |
5 | 2007-08-25 | Colour 5 |
11 | 2007-09-25 | Colour 1 |
13 | 2007-07-19 | Colour 3 |
14 | 2007-11-08 | Colour 4 |
15 | 2007-11-03 | Colour 5 |
16 | 2007-06-04 | Colour 1 |
17 | 2007-07-04 | Colour 2 |
18 | 2007-07-03 | Colour 3 |
19 | 2007-08-04 | Colour 4 |
20 | 2007-08-20 | Colour 5 |
21 | 2007-11-20 | Colour 1 |
22 | 2007-08-16 | Colour 2 |
25 | 2007-12-19 | Colour 5 |
26 | 2007-12-25 | Colour 1 |
27 | 2007-11-29 | Colour 2 |
28 | 2007-06-19 | Colour 3 |
29 | 2007-11-21 | Colour 4 |
31 | 2007-11-07 | Colour 1 |
33 | 2007-12-14 | Colour 3 |
34 | 2007-06-23 | Colour 4 |
35 | 2007-08-16 | Colour 5 |
36 | 2007-11-24 | Colour 1 |
38 | 2007-06-29 | Colour 3 |
41 | 2007-07-30 | Colour 1 |
47 | 2007-06-29 | Colour 2 |
48 | 2007-07-23 | Colour 3 |
49 | 2007-10-12 | Colour 4 |
... | ... | ... |
51 | 2007-10-29 | Colour 1 |
52 | 2007-07-05 | Colour 2 |
53 | 2007-09-14 | Colour 3 |
56 | 2007-10-13 | Colour 1 |
57 | 2007-09-18 | Colour 2 |
58 | 2007-11-12 | Colour 3 |
59 | 2007-11-26 | Colour 4 |
63 | 2007-07-05 | Colour 3 |
64 | 2007-10-09 | Colour 4 |
65 | 2007-11-20 | Colour 5 |
66 | 2007-08-01 | Colour 1 |
67 | 2007-12-16 | Colour 2 |
71 | 2007-12-02 | Colour 1 |
73 | 2007-09-15 | Colour 3 |
75 | 2007-09-10 | Colour 5 |
77 | 2007-11-14 | Colour 2 |
78 | 2007-12-31 | Colour 3 |
79 | 2007-06-14 | Colour 4 |
83 | 2007-11-26 | Colour 3 |
84 | 2007-08-05 | Colour 4 |
85 | 2007-08-25 | Colour 5 |
86 | 2007-07-05 | Colour 1 |
87 | 2007-07-31 | Colour 2 |
88 | 2007-09-21 | Colour 3 |
89 | 2007-09-10 | Colour 4 |
91 | 2007-12-11 | Colour 1 |
92 | 2007-12-14 | Colour 2 |
93 | 2007-11-02 | Colour 3 |
94 | 2007-10-25 | Colour 4 |
98 | 2007-07-24 | Colour 3 |
runif
)matti = matrix(runif(50,0,10),nrow=10)
matti
9.804293 | 4.9836626 | 0.7089561 | 0.64035735 | 8.655358 |
1.535470 | 6.3303802 | 9.0285600 | 9.58436689 | 2.117108 |
7.054454 | 0.2246136 | 6.6611312 | 6.70334340 | 7.309426 |
4.752587 | 6.0213062 | 7.7713002 | 0.07117314 | 3.526904 |
3.143838 | 5.7364747 | 0.4074274 | 4.43395158 | 7.922231 |
3.176729 | 1.3561584 | 2.1107383 | 2.28420872 | 6.819195 |
5.302103 | 9.8258713 | 9.2962555 | 4.64784942 | 3.751291 |
5.985252 | 2.3348505 | 7.1433309 | 0.54225692 | 6.742854 |
7.494877 | 1.6589038 | 0.6764284 | 9.18624923 | 9.676256 |
1.905350 | 7.5088342 | 9.7672090 | 9.56431339 | 5.387983 |
for
to calculate the column means of
the matrix.for (i in 1:5) mean(matti[,i])
apply(matti,2,mean)
matti[seq(2,10,2),]
1.535470 | 6.330380 | 9.028560 | 9.58436689 | 2.117108 |
4.752587 | 6.021306 | 7.771300 | 0.07117314 | 3.526904 |
3.176729 | 1.356158 | 2.110738 | 2.28420872 | 6.819195 |
5.985252 | 2.334851 | 7.143331 | 0.54225692 | 6.742854 |
1.905350 | 7.508834 | 9.767209 | 9.56431339 | 5.387983 |