[R] How to extract Friday data from daily data.
Gabor Grothendieck
ggrothendieck at gmail.com
Wed Nov 10 05:09:45 CET 2010
On Tue, Nov 9, 2010 at 11:23 AM, thornbird <huachang396 at gmail.com> wrote:
>
> Thanks for your suggestion. I received a few error messages. Can anyone help
> me figure out why I get the following error messages and how to solve them
> by revising my code? The data is shown in the post above. Thanks.
>
>
>> library(zoo)
>>
>> colClasses <- c("character", "character", "numeric", "character",
>> "character", "numeric")
>>
>> col.names <- c("date", "day", "time", "webpage", "item", "value")
>>
>> # z <- read.zoo("myfile.dat", skip = 1, index = as.list(1:5),
>> z <- read.zoo(testdata[,1:5],as.Date(as.character(testdata[, 1])),skip =
>> 1, index = as.list(1:6),
> + format = "%d/%m/%Y", col.names = col.names,
> + aggregate = function(x) tail(x, 1), colClasses = colClasses)
> Error in `[.data.frame`(rval, , j) : undefined columns selected
>>
>>
>> # extract all Thursdays and Fridays
>> z45 <- z[format(time(z), "%w") %in% 4:5,]
> Error: object 'z' not found
>>
>> # keep last entry in each week
>> # and show result on R console
>> z45[!duplicated(format(time(z45), "%U"), fromLast = TRUE), ]
> Error: object 'z45' not found
Read ?read.zoo and the three zoo vignettes (pdf documents).
Assuming we wish to read in the date and value column the following
first writes out a test file and then uses read.zoo to read it back
in. If there are multiple records at the same date we only keep the
last (see aggregate= argument). Then we perform the extraction.
# create some data and write out a file with it
Lines <- "date day time webpage item value
1 10-Jun-09 Wed 9:40:53 MattDamon fans 613031
2 11-Jun-09 Thu 5:27:47 MattDamon fans 630242
3 11-Jun-09 Thu 9:23:23 MattDamon fans 631966
4 11-Jun-09 Thu 22:11:33 MattDamon fans 642045
5 13-Jun-09 Sat 13:07:53 MattDamon fans 669791
6 15-Jun-09 Mon 5:07:06 MattDamon fans 700180
7 15-Jun-09 Mon 13:09:43 MattDamon fans 702949
8 16-Jun-09 Tue 22:45:27 MattDamon fans 726624
9 17-Jun-09 Wed 13:08:19 MattDamon fans 734412
10 19-Jun-09 Fri 12:37:09 MattDamon fans 765057
11 20-Jun-09 Sat 12:38:36 MattDamon fans 782741
12 21-Jun-09 Sun 12:36:25 MattDamon fans 796054
13 22-Jun-09 Mon 12:39:36 MattDamon fans 809816
14 23-Jun-09 Tue 12:40:25 MattDamon fans 833996
15 27-Jun-09 Sat 12:40:36 MattDamon fans 899900
16 28-Jun-09 Sun 12:38:07 MattDamon fans 910401
17 10-Jun-09 Wed 9:40:53 MattDamon views 613031
18 11-Jun-09 Thu 5:27:47 MattDamon views 630242
19 11-Jun-09 Thu 9:23:23 MattDamon views 631966
20 11-Jun-09 Thu 22:11:33 MattDamon views 642045
21 13-Jun-09 Sat 13:07:53 MattDamon views 669791
22 15-Jun-09 Mon 5:07:06 MattDamon views 700180
23 15-Jun-09 Mon 13:09:43 MattDamon views 702949
24 16-Jun-09 Tue 22:45:27 MattDamon views 726624
25 17-Jun-09 Wed 13:08:19 MattDamon views 734412
26 19-Jun-09 Fri 12:37:09 MattDamon views 765057
27 20-Jun-09 Sat 12:38:36 MattDamon views 782741
28 21-Jun-09 Sun 12:36:25 MattDamon views 796054
29 22-Jun-09 Mon 12:39:36 MattDamon views 809816
30 23-Jun-09 Tue 12:40:25 MattDamon views 833996
31 25-Jun-09 Thu 12:38:27 MattDamon views 871237
32 26-Jun-09 Fri 12:36:12 MattDamon views 887175
33 26-Jun-09 Fri 13:36:12 MattDamon views 887195
34 27-Jun-09 Sat 12:40:36 MattDamon views 899900
35 10-Jun-09 Wed 9:40:53 AngieeJolie fans 613031
36 11-Jun-09 Thu 5:27:47 AngieeJolie fans 630242
37 11-Jun-09 Thu 9:23:23 AngieeJolie fans 631966
38 11-Jun-09 Thu 22:11:33 AngieeJolie fans 642045
39 13-Jun-09 Sat 13:07:53 AngieeJolie fans 669791
40 15-Jun-09 Mon 5:07:06 AngieeJolie fans 700180
41 16-Jun-09 Tue 22:45:27 AngieeJolie fans 726624
42 17-Jun-09 Wed 13:08:19 AngieeJolie fans 734412
43 19-Jun-09 Fri 12:37:09 AngieeJolie fans 765057
44 20-Jun-09 Sat 12:38:36 AngieeJolie fans 782741
45 21-Jun-09 Sun 12:36:25 AngieeJolie fans 796054
46 22-Jun-09 Mon 12:39:36 AngieeJolie fans 809816
47 23-Jun-09 Tue 12:40:25 AngieeJolie fans 833996
48 25-Jun-09 Thu 12:38:27 AngieeJolie fans 871237
49 26-Jun-09 Fri 12:36:12 AngieeJolie fans 887175
50 26-Jun-09 Fri 13:36:12 AngieeJolie fans 887195
51 30-Jun-09 Tue 12:36:43 AngieeJolie fans 934280
52 10-Jun-09 Wed 9:40:53 AngieeJolie views 613031
53 11-Jun-09 Thu 5:27:47 AngieeJolie views 630242
54 11-Jun-09 Thu 9:23:23 AngieeJolie views 631966
55 11-Jun-09 Thu 22:11:33 AngieeJolie views 642045
56 13-Jun-09 Sat 13:07:53 AngieeJolie views 669791
57 15-Jun-09 Mon 5:07:06 AngieeJolie views 700180
58 15-Jun-09 Mon 13:09:43 AngieeJolie views 702949
59 16-Jun-09 Tue 22:45:27 AngieeJolie views 726624
60 17-Jun-09 Wed 13:08:19 AngieeJolie views 734412
61 19-Jun-09 Fri 12:37:09 AngieeJolie views 765057
62 20-Jun-09 Sat 12:38:36 AngieeJolie views 782741
63 21-Jun-09 Sun 12:36:25 AngieeJolie views 796054
64 22-Jun-09 Mon 12:39:36 AngieeJolie views 809816
65 23-Jun-09 Tue 12:40:25 AngieeJolie views 833996
66 25-Jun-09 Thu 12:38:27 AngieeJolie views 871237
67 26-Jun-09 Fri 12:36:12 AngieeJolie views 887175
68 26-Jun-09 Fri 13:36:12 AngieeJolie views 887195
69 27-Jun-09 Sat 12:40:36 AngieeJolie views 899900"
tmp <- read.table(textConnection(Lines))
write.table(tmp, sep = ",", file = "testdata.csv", row.names = FALSE)
# file.show("testdata.csv")
# use read.zoo to read in "testdata.csv"
library(zoo)
z <- read.zoo("testdata.csv", header = TRUE, sep = ",",
format = "%d-%b-%y", aggregate = function(x) tail(x, 1),
colClasses = c("character", "NULL", "NULL", "NULL", "NULL", "numeric"))
z[format(time(z), "%w") %in% 4:5]
--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com
More information about the R-help
mailing list