271

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 add another column with the day of the week associated with the date? The dataset is really large, so it would not make sense to go through manually and make the changes.

df = data.frame(date=c("2012-02-01", "2012-02-01", "2012-02-02")) 

So after adding the days, it would end up looking like:

   date       day
2012-02-01   Wednesday
2012-02-01   Wednesday
2012-02-02   Thursday

Is this possible? Can anyone point me to a package that will allow me to do this? Just trying to automatically generate the day by the date.

Henrik
  • 65,555
  • 14
  • 143
  • 159
ATMathew
  • 12,566
  • 26
  • 69
  • 76
  • somewhat related https://stackoverflow.com/questions/16193549/how-can-i-create-a-vector-containing-the-days-of-the-week – tjebo May 26 '22 at 21:23

7 Answers7

362
df = data.frame(date=c("2012-02-01", "2012-02-01", "2012-02-02")) 
df$day <- weekdays(as.Date(df$date))
df
##         date       day
## 1 2012-02-01 Wednesday
## 2 2012-02-01 Wednesday
## 3 2012-02-02  Thursday

Edit: Just to show another way...

The wday component of a POSIXlt object is the numeric weekday (0-6 starting on Sunday).

as.POSIXlt(df$date)$wday
## [1] 3 3 4

which you could use to subset a character vector of weekday names

c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", 
    "Friday", "Saturday")[as.POSIXlt(df$date)$wday + 1]
## [1] "Wednesday" "Wednesday" "Thursday" 
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
GSee
  • 48,880
  • 13
  • 125
  • 145
  • 3
    +1 Is there a way to use `weekdays` to get the number of weekday as you do using `as.POSIXlt`?? – Shambho Jun 04 '14 at 17:40
  • 3
    @Shambho I guess you could do this: `setNames(0:6, c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"))[weekdays(as.Date(df$date))]`. If you don't like the names, you could wrap `unname()` around it. – GSee Jun 04 '14 at 22:21
  • 6
    To get the weekday number (0-6,Sun-Sat) from the date you can do: format(as.Date(df$date),"%w") . For the format code details see http://www.stat.berkeley.edu/~s133/dates.html – JStrahl Apr 24 '15 at 22:44
97

Use the lubridate package and function wday:

library(lubridate)
df$date <- as.Date(df$date)
wday(df$date, label=TRUE)
[1] Wed   Wed   Thurs
Levels: Sun < Mon < Tues < Wed < Thurs < Fri < Sat
Andrie
  • 176,377
  • 47
  • 447
  • 496
  • 14
    The good thing about this approach is that it returns the days as a factor, so if you create a chart the days will be in the correct order. – bobfet1 May 06 '13 at 19:03
  • 3
    To get the full word for each day (e.g. Sunday instead of Sun): `abbr = FALSE` – stevec Apr 20 '20 at 20:08
78

Look up ?strftime:

%A Full weekday name in the current locale

df$day = strftime(df$date,'%A')
Henrik
  • 65,555
  • 14
  • 143
  • 159
nograpes
  • 18,623
  • 1
  • 44
  • 67
  • 20
    In case somebody searched for a weekday number - use `'%u'` instead of `'%A'` – Vlad Holubiev Dec 15 '15 at 16:51
  • If you want the numerical day of the week, don't forget to use the original Date object, not the strftime result because it's of type character – FLonLon Feb 08 '21 at 13:44
18

Let's say you additionally want the week to begin on Monday (instead of default on Sunday), then the following is helpful:

require(lubridate)
df$day = ifelse(wday(df$time)==1,6,wday(df$time)-2)

The result is the days in the interval [0,..,6].

If you want the interval to be [1,..7], use the following:

df$day = ifelse(wday(df$time)==1,7,wday(df$time)-1)

... or, alternatively:

df$day = df$day + 1
Peter Lustig
  • 941
  • 11
  • 23
  • 7
    You can also use the argument `week_start`: `wday(df$date, label = TRUE, week_start = 1)` – mrub May 23 '18 at 20:17
14

This should do the trick

df = data.frame(date=c("2012-02-01", "2012-02-01", "2012-02-02")) 
dow <- function(x) format(as.Date(x), "%A")
df$day <- dow(df$date)
df

#Returns:
        date       day
1 2012-02-01 Wednesday
2 2012-02-01 Wednesday
3 2012-02-02  Thursday
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
10
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
s_scolary
  • 1,361
  • 10
  • 21
4

form comment of JStrahl format(as.Date(df$date),"%w"), we get number of current day : as.numeric(format(as.Date("2016-05-09"),"%w"))

Qbik
  • 5,885
  • 14
  • 62
  • 93