0

I have a large df with many columns, here's a sample:

df <- data.frame(Quarter = c["Q4 2022", "Q4 2022", "Q1 2023", "Q1 2023"],                     
                    Date = c["2022-11-08", "2022-11-25", "02/09/2023", "03/15/2023"])

I want to change the "Date" column to a date field in the format YYYY-mm-dd (e.g. 2022-11-08). It's currently a character type.

I've tried the following:

df$`Date` <- as.Date(df$`Date`, "%Y-%m-%d")

but it ends up actually just dropping the last values in the column that are in the mm/dd/yyyy format and I end up with this:

Date = c["2022-11-08", "2022-11-25", NA, NA]

Anyone know why it's behaving this way?

jpsmith
  • 11,023
  • 5
  • 15
  • 36
Donut
  • 11
  • 4
  • 3
    your column had mixed formats. Try `library(parsedate);as.Date(parse_date(df$Date))#[1] "2022-11-08" "2022-11-25" "2023-02-09" "2023-03-15"` – akrun Apr 27 '23 at 13:00

0 Answers0