Can't set number of day and name of day in R (date always character)

Viewed 58

Sample data to reproduce

a=structure(list(date = c("01.01.2021", "02.01.2021", "03.01.2021", 
"04.01.2021", "05.01.2021", "06.01.2021", "07.01.2021", "08.01.2021", 
"09.01.2021", "10.01.2021", "11.01.2021", "12.01.2021", "13.01.2021", 
"14.01.2021", "15.01.2021", "16.01.2021", "17.01.2021", "18.01.2021", 
"19.01.2021", "20.01.2021"), sales_count = c(10L, 4L, 8L, 6L, 
4L, 4L, 4L, 3L, 1L, 5L, 2L, 10L, 5L, 9L, 2L, 8L, 5L, 6L, 8L, 
3L)), class = "data.frame", row.names = c(NA, -20L))

Why when i load data, the date is character? i get the error. My attempt

a$date = as.Date (a$date) l Error in charToDate (x): character string is not in a standard unambiguous format

Error in charToDate (x):
   character string is not in a standard unambiguous format

I saw many similar posts in SO, but that solutions didn't help me. How fix this error, that date was

date     : Date, format: "2021-01-20" ...

and then how for each date assign name of date and number of day? Indeed my desired output for example

sales_count date    day_num weekday
8        2021-01-20   3 Wednesday

Thank you.

2 Answers

You could use the following solution:

library(dplyr)

a %>%
  mutate(date = as.Date(date, "%d.%m.%Y"), 
         weekday = weekdays(date))

         date sales_count   weekday
1  2021-01-01          10    Friday
2  2021-01-02           4  Saturday
3  2021-01-03           8    Sunday
4  2021-01-04           6    Monday
5  2021-01-05           4   Tuesday
6  2021-01-06           4 Wednesday
7  2021-01-07           4  Thursday
8  2021-01-08           3    Friday
9  2021-01-09           1  Saturday
10 2021-01-10           5    Sunday
11 2021-01-11           2    Monday
12 2021-01-12          10   Tuesday
13 2021-01-13           5 Wednesday
14 2021-01-14           9  Thursday
15 2021-01-15           2    Friday
16 2021-01-16           8  Saturday
17 2021-01-17           5    Sunday
18 2021-01-18           6    Monday
19 2021-01-19           8   Tuesday
20 2021-01-20           3 Wednesday

Alternatively we could use lubridate package. We should set the label argument of wday function which is an equivalent to base::weekdays to TRUE, otherwise it returns the number of weekday starting from sunday:

library(lubridate)

a %>%
  mutate(date = dmy(date), 
         weekday = wday(date, label = TRUE))

Using base R

within(a, {date <- as.Date(date, "%d.%m.%Y"); weekday <- weekdays(date)})
         date sales_count   weekday
1  2021-01-01          10    Friday
2  2021-01-02           4  Saturday
3  2021-01-03           8    Sunday
4  2021-01-04           6    Monday
5  2021-01-05           4   Tuesday
6  2021-01-06           4 Wednesday
7  2021-01-07           4  Thursday
8  2021-01-08           3    Friday
9  2021-01-09           1  Saturday
10 2021-01-10           5    Sunday
11 2021-01-11           2    Monday
12 2021-01-12          10   Tuesday
13 2021-01-13           5 Wednesday
14 2021-01-14           9  Thursday
15 2021-01-15           2    Friday
16 2021-01-16           8  Saturday
17 2021-01-17           5    Sunday
18 2021-01-18           6    Monday
19 2021-01-19           8   Tuesday
20 2021-01-20           3 Wednesday
Related