Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

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?

like image 653
outis Avatar asked Apr 24 '13 13:04

outis


1 Answers

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"
like image 194
GKi Avatar answered Oct 14 '22 09:10

GKi