Convert chr to date in R, but have same date

Viewed 52

I have data like:

         date       tot     rat
1  2022-08-04       637090 0.128866
2  2022-08-05      1438100 0.117801
3  2022-08-06      1270188 0.133178
4  2022-08-07      2142400 0.121704
5  2022-09-01      2141110 0.111704

I use R studio 4.1.1. column date is chr and I want to convert as date. Here's the script I try:

library(lubridate)
date_prep <- data %>% 
  mutate(date = ymd(date) %>% as_date()) %>% 
  select(date, tot, rat)

but the result:

         date        tot      rat
1  2022-08-01       637090 0.128866
2  2022-08-01      1438100 0.117801
3  2022-08-01      1270188 0.133178
4  2022-08-01      2142400 0.121704
5  2022-09-01      2141110 0.111704

why the date is same? any suggestion how to convert chr to date?

1 Answers
data$date = as.POSIXct(data$date, format="%Y-%m-%d")
Related