How can I create a vector containing the days of the week?

前端 未结 4 1974
时光取名叫无心
时光取名叫无心 2021-02-20 12:07

I need a vector containing the days of the week very often, but I always type it out:

days.of.week <- c(\"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\",          


        
相关标签:
4条回答
  • 2021-02-20 12:22

    There you go, the vector of weekdays "Monday", ..., "Sunday":

    days.of.week <- weekdays(x=as.Date(seq(7), origin="1950-01-01"))
    
    0 讨论(0)
  • 2021-02-20 12:30

    Based on today's date we can also find days of a week

    weekdays(as.Date(seq(7),origin=Sys.Date() - as.POSIXlt(Sys.Date())$wday ))
     [1] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"    "Saturday" 
     [7] "Sunday"
    
    0 讨论(0)
  • 2021-02-20 12:40

    One possibility:

    days.of.week <- weekdays(Sys.Date()+0:6)
    

    Always starting on Monday:

    days.of.week <- weekdays(as.Date(4,"1970-01-01",tz="GMT")+0:6)
    

    Or you could just define it as you have, but in your .Rprofile, so it's always available on startup.

    0 讨论(0)
  • 2021-02-20 12:45

    While the function-based answers are slick, Joshua's last comment is spot-on. If you've got a variable you use regularly, either create it in your .Rprofile or load it from an .Rdata file, using some line in .Rprofile like load('daysofweek.rdata') .

    Note that changing the first day of the week is as simple as

    neworder <- days.of.week[c(2:7,1)]

    0 讨论(0)
提交回复
热议问题