0

I have my dates formatted as 'YYYYMMDD' like '20150531' but now I want to separate my data into categories for the 7 days of the week by creating another variable called Day. How could I do this in R?

Phil
  • 7,287
  • 3
  • 36
  • 66
Red
  • 47
  • 5

2 Answers2

0

We can convert to Date class and then use format to get the weekday name in base R

df1$Weekday <- format(as.Date(df1$date , '%Y%m%d'), '%a')

-output

df1
#     date Weekday
#1 2015031     Sun

data

df1 <- data.frame(date = '2015031')
akrun
  • 874,273
  • 37
  • 540
  • 662
0

You can try weekdays() function from base R:

#Data
df <- data.frame(Date='2015031',stringsAsFactors = F)
df$Weekday <- weekdays(as.Date(df$Date,'%Y%m%d'))

Output:

df
     Date Weekday
1 2015031  Sunday
Duck
  • 39,058
  • 13
  • 42
  • 84