Find the day of a week

前端 未结 7 1925
长情又很酷
长情又很酷 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条回答
  •  猫巷女王i
    2020-11-22 17:44

    start = as.POSIXct("2017-09-01")
    end = as.POSIXct("2017-09-06")
    
    dat = data.frame(Date = seq.POSIXt(from = start,
                                       to = end,
                                       by = "DSTday"))
    
    # see ?strptime for details of formats you can extract
    
    # day of the week as numeric (Monday is 1)
    dat$weekday1 = as.numeric(format(dat$Date, format = "%u"))
    
    # abbreviated weekday name
    dat$weekday2 = format(dat$Date, format = "%a")
    
    # full weekday name
    dat$weekday3 = format(dat$Date, format = "%A")
    
    dat
    # returns
        Date       weekday1 weekday2  weekday3
    1 2017-09-01        5      Fri    Friday
    2 2017-09-02        6      Sat    Saturday
    3 2017-09-03        7      Sun    Sunday
    4 2017-09-04        1      Mon    Monday
    5 2017-09-05        2      Tue    Tuesday
    6 2017-09-06        3      Wed    Wednesday
    

提交回复
热议问题