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

thornbird huachang396 at gmail.com
Fri Nov 5 21:10:41 CET 2010


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. 



testdata$date = as.Date(testdata$date,"%m/%d/%Y") 

Thudat = subset(testdata,day=="Thu") 
Fridat = subset(testdata,day=="Fri") 

Friday_dates = Thudat$date+1 

Friday_info = NULL 

for(i in 1:length(Friday_dates)){ 

temp = subset(Fridat,date==Friday_dates[i]) # select the Friday dates from 
Fridat 

if(nrow(temp)>0){ # if that Friday date value exists in Friday 

Friday_info = rbind(Friday_info,temp[nrow(temp),]) # by saying nrow(temp) 
with the data organized chronologically already, you don't have to add an 
additional if      statement for multiple measurements in the same day. 

} else { # if that Friday date value doesn't exist in Fridat 

Friday_info = rbind(Friday_info,Thudat[i,]) # choosing the date from Thudat 
instead. 

} 

} 
-- 
View this message in context: http://r.789695.n4.nabble.com/How-to-extract-Friday-data-from-daily-data-tp3029050p3029328.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list