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\",
There you go, the vector of weekdays "Monday", ..., "Sunday":
days.of.week <- weekdays(x=as.Date(seq(7), origin="1950-01-01"))
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"
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.
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)]