Find most recent Monday for a dataframe

前端 未结 6 592
甜味超标
甜味超标 2021-01-15 02:35

I have a dataframe object, and among the fields in it, I have a dates:

df$dates

I need to add a column which is \'Week Starting\', i.e.

6条回答
  •  不知归路
    2021-01-15 03:04

    a[1] <-as.Date("2016-08-20")
    

    Finding Next day (Here "Monday")

    a[1] + match("Monday",weekdays(seq(a[1]+1, a[1]+6,"days")))
    

    "2016-08-22"

    Finding Last Day (Here "Friday")

    a[1] + (match("Friday",weekdays(seq(a[1]+1, a[1]+6,"days")))-7)
    

    "2016-08-19"

提交回复
热议问题