[R] How to extract Friday data from daily data.

Marianne Promberger marianne.promberger at kcl.ac.uk
Tue Nov 9 14:24:28 CET 2010


thornbird <huachang396 at gmail.com> 05-Nov-10 20:10:
> 
> Thank you very much. It worked great with the testdata. I have one more
> questionto to ask. As my data is incomplete, sometimes Thu is also missing,
> then I have no other options but to pick Sat instead, and if Sat is also
> missing, then my best possible option is to pick Wed, and etc. Bascially I
> have to pick a day as the data for that week starting from Friday following
> this order: 
> 
> Fri--> (if no Fri) Thu--> (if no Thu) Sat--> (if no Sat) Wed --> (if no Wed)
> Sun --> (if no Sun) Tue -->(if no Tue) Mon. 
> 
> In this sense, I have to write a loop if command, right? Could you please
> help me with that? Again thanks a lot. 

I don't think you need a loop. 

Without knowing zoo, here's what I would try: 

If you look at Gabor's code,

>> # extract all Thursdays and Fridays
>> z45 <- z[format(time(z), "%w") %in% 4:5,]

>> # keep last entry in each week
>> # and show result on R console
>> z45[!duplicated(format(time(z45), "%U"), fromLast = TRUE), ]

It takes Thur and Fri data and takes the latest record that is there.

You could work on a solution along the same lines except have the days
ordered in the order of your "top choice" above (or inverse), then
pick the first (or last) entry that exists for that week. Of course,
since your choice order is not chronologlical you probably can't use
"fromLast". 

Marianne


-- 
Marianne Promberger PhD, King's College London
http://promberger.info
R version 2.12.0 (2010-10-15)
Ubuntu 9.04



More information about the R-help mailing list