Find the day of a week
Asked Answered
C

7

273

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.

Ceasefire answered 9/2, 2012 at 17:54 Comment(1)
somewhat related #16194049Michelle
B
364
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" 
Boutte answered 9/2, 2012 at 18:6 Comment(3)
+1 Is there a way to use weekdays to get the number of weekday as you do using as.POSIXlt??Pegues
@Pegues 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.Boutte
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 stat.berkeley.edu/~s133/dates.htmlCadmium
C
99

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
Cohby answered 9/2, 2012 at 17:58 Comment(2)
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.Quinze
To get the full word for each day (e.g. Sunday instead of Sun): abbr = FALSEAutoionization
B
79

Look up ?strftime:

%A Full weekday name in the current locale

df$day = strftime(df$date,'%A')
Boonie answered 9/2, 2012 at 17:58 Comment(2)
In case somebody searched for a weekday number - use '%u' instead of '%A'Overprize
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 characterPiggott
L
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
Lozoya answered 20/3, 2014 at 20:54 Comment(1)
You can also use the argument week_start: wday(df$date, label = TRUE, week_start = 1)Weariful
H
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
Herwin answered 9/2, 2012 at 18:6 Comment(0)
N
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
Nephrosis answered 28/9, 2017 at 14:38 Comment(0)
C
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"))

Catherinacatherine answered 9/5, 2016 at 13:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.