I want to find the week ending Fridays from the dates available in DT_SK
column, say if may2014 ends on 31st which is a Saturday, I would like to get the data for week that ends of 6th June 2014 which is a Friday.
Data1[,c("p_num","DT_SK", "Tr")]
the data looks like
P_num DT_SK Tr
00915 20130920 1.124
1790050 20140110 1.053
1798001 20131025 1.276
2331001 20140314 1.072
233300 20140124 1.106
233302 20140124 1.106
233305 20140124 1.106
The code is something like this, is there a better way to pull the data
lDTSK = as.data.frame(list("Ori" = levels(factor(data1$DT_SK))))
lDTSK$New = "0"
for (i in 1:nrow(lDTSK)){
#i=1
strWeekDay = as.character(weekdays(as.Date(paste(
substr(lDTSK$Ori[i],1,4),
substr(lDTSK$Ori[i],5,6),
substr(lDTSK$Ori[i],7,8)
,sep="-"))))
if (strWeekDay !="Friday") {
lDTSK$New[i] = as.character(lDTSK$Ori[i+1])
}else{
lDTSK$New[i] = as.character(lDTSK$Ori[i])
}
}
lDTSK$New = paste("TRx_",lDTSK$New,sep="")