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", "Friday", "Saturday", "Sunday")
This is pretty easy because it's short, but there's always the possibility of typos. Is there a way to create a vector containing the days of the week programmatically?
An alternative is to use weekdays
with ISOdate
:
weekdays(ISOdate(1, 1, 1:7))
#[1] "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday" "Sunday"
And to get it in other languages Sys.setlocale
could be used.
Sys.setlocale("LC_TIME", "de_DE.UTF-8")
#[1] "de_DE.UTF-8"
weekdays(ISOdate(1, 1, 1:7))
#[1] "Montag" "Dienstag" "Mittwoch" "Donnerstag" "Freitag" "Samstag" "Sonntag"
Also its possible to use format
format(ISOdate(1, 1, 1:7), "%A")
#[1] "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday" "Sunday"
or get abbreviated weekday names
format(ISOdate(1, 1, 1:7), "%a")
#weekdays(ISOdate(1, 1, 1:7), TRUE) #Alternative
#[1] "Mon" "Tue" "Wed" "Thu" "Fri" "Sat" "Sun"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With