Issue
This Content is from Stack Overflow. Question asked by a_p27
I am trying to convert the values in column date (the data below) into date format yyyy-mm-dd:
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_df = index_prices |>
filter(month(date) == 6) |>
mutate(sorting_date = date %m+% months(1)) |>
select(symbol, sorting_date, me_df = adjusted)
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!
Solution
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.
This Question was asked in StackOverflow by a_p27 and Answered by Karsten W. It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.