Home > Software engineering >  Date format converting in R
Date format converting in R

Time:09-19

I am trying to convert the values in column date (the data below) into date format yyyy-mm-dd:

enter image description here

I did it by using as.Date() and then change the output list into a dataframe as follows:

date_new = as.Date(df$Date, origin = '1899-12-30')
better_date = data.frame(Date = unlist(date_new))

I continue to use the converted data to filter June in each year and some other tasks as follows:

 me_ff = df |>
   filter(month(better_date) == 6) |>
   mutate(sorting_date = better_date %m % months(1)) |>
   select(ticker,sorting_date,me_ff = Mkt_cap)

and the error message is:

"Error in `filter()`:
! Problem while computing `..1 = month(better_date) == 6`.
Caused by error in `as.POSIXlt.default()`:
! do not know how to convert 'x' to class “POSIXlt”
Run `rlang::last_error()` to see where the error occurred."

Could you please help me to solve the problem?

Thank you so much for your help!

CodePudding user response:

Looks to me as if you confuse the data.frame object (the "table") with the date column. Untested code:

df <- transform(df, Date=as.Date(Date, origin = '1899-12-30')) |>
subset(strftime(Date, "%M")=="06")

should select the June rows.

  • Related