Find the day of a week

前端 未结 7 1913
长情又很酷
长情又很酷 2020-11-22 17:37

Let\'s say that I have a date in R and it\'s formatted as follows.

   date      
2012-02-01 
2012-02-01
2012-02-02

Is there any way in R to

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 17:41

    Let's say you additionally want the week to begin on Monday (instead of default on Sunday), then the following is helpful:

    require(lubridate)
    df$day = ifelse(wday(df$time)==1,6,wday(df$time)-2)
    

    The result is the days in the interval [0,..,6].

    If you want the interval to be [1,..7], use the following:

    df$day = ifelse(wday(df$time)==1,7,wday(df$time)-1)
    

    ... or, alternatively:

    df$day = df$day + 1
    

提交回复
热议问题