[R] How to extract data for the week ending on Friday from date column.

arun smartpink111 at yahoo.com
Wed Jun 4 19:54:42 CEST 2014


Hi,

Using the example data,
library(zoo) 
z1 <- read.zoo("dfRaw_20140509.csv",sep="",index.column=2,format="%Y%m%d")
z1
#           P_num    P_ID Totalvalue
#2013-05-10 25193 1230238      1.203
#2013-05-24 25190 1230238      1.201
#2013-07-05 25191 1230238      1.208
#2013-08-02 25194 1230238      1.195
#2013-09-06 25192 1230238      1.209
#2013-09-20   915 1230238      1.124
#2013-10-25 17980 1230238      1.276
#2013-11-08 23316 1230238      1.431
#2013-11-29  3817 1230238      2.082
#2014-01-10 17901 1230238      1.053
#2014-01-24 23315 1230238      1.106
#2014-02-14 32554 1230238      1.493
#2014-03-14 23311 1230238      1.072
#2014-03-14 25885 1230238      1.000
#2014-03-21 32550 1230238      1.383
#2014-05-09 32551 1230238      1.309

time(z1)[8] <- "2013-10-28"
format(time(z1), "%w")
 #[1] "5" "5" "5" "5" "5" "5" "5" "1" "5" "5" "5" "5" "5" "5" "5" "5"

 z1[format(time(z1), "%w") %in% 5,]


A.K.


The data loks like this 
        P_num       Date      P_ID      Totalvalue 
1    00915    20130920    1230238    1.124 
2    17901    20140110    1230238    1.053 
3    17980    20131025    1230238    1.276 
4    23311    20140314    1230238    1.072 
5    23315    20140124    1230238    1.106 
6    23316    20131108    1230238    1.431 
7    25190    20130524    1230238    1.201 
8    25191    20130705    1230238    1.208 
9    25192    20130906    1230238    1.209 
10    25193    20130510    1230238    1.203 
11    25194    20130802    1230238    1.195 
12    25885    20140314    1230238    1 
13    32550    20140321    1230238    1.383 
14    32551    20140509    1230238    1.309 
15    32554    20140214    1230238    1.493 
16    3817    20131129    1230238    2.082 

I have used the ZOO package but still I am getting an error. Can you please help. 

library(zoo) 
z <- read.zoo("dfRaw_20140509.csv", header = TRUE, sep = 
",",format = "%d-%b-%y",colClasses = c("numeric", "numeric", 
"character", "character", "numeric", "numeric")) 
z[format(time(z), "%w") %in% 2,] 
Error in `[.data.frame`(rval, , j) : undefined columns selected



More information about the R-help mailing list