1

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="")
MrFlick
  • 195,160
  • 17
  • 277
  • 295
user3703195
  • 61
  • 1
  • 8
  • Please provide a [reproducible example](http://stackoverflow.com/q/5963269/489704). We don't know what column `Dt_SK` looks like. Also, your question is unclear - what do you mean by "the week ending on Fridays"? – jbaums Jun 03 '14 at 13:26
  • Do you know how to (in general) select rows of a data.frame satisfying a condition? – Hugh Jun 03 '14 at 13:29
  • There is a function `nextfri` in the zoo package's zoo-quickref vignette: http://cran.r-project.org/web/packages/zoo/vignettes/zoo-quickref.pdf that may help. – G. Grothendieck Jun 03 '14 at 13:46
  • every week ends on Friday. Do you want the last or first complete week lying in a special month? – nnn Jun 03 '14 at 14:32
  • Hi nnn, i would like to find the last-day of each week (that is Friday). – user3703195 Jun 04 '14 at 08:02

0 Answers0