How to find Previous Sunday in R

后端 未结 4 684
说谎
说谎 2020-12-16 14:45

It seems the Internet has not answered this question for R yet:

If I have a date. Say the 20th of march: as.Date(\"2015-03-20\") how do I get, in R, the previous Sun

4条回答
  •  温柔的废话
    2020-12-16 14:53

    One way:

    d<-as.Date("2015-03-20")
    d-as.POSIXlt(d)$wday
    ## [1] "2015-03-15"
    

    There;s also the more hackish way, using the fact that dates are represented as integers with day zero being a Thursday (Jan 1 1970):

    d-((as.numeric(d)+4)%% 7)
    ## [1] "2015-03-15"
    

提交回复
热议问题