Date Problems in R: replacement has x rows, data has y

Viewed 25

I have an Excel Sheet (csv) in which the dates appear like this : 2015-01-29 (Year, Month, Date)

When I import this sheet into R, the dates automatically appear as "factor" type. Since I am using an older version of R (3.5.2) in which there are certain problems with factors/strings, I tried to convert the date using the following code:

file$date_var  = as.Date(as.character(file$date_var), format = "%Y/%m/%d")

Although this code produces no error messages, when I try to perform some operation on this date variable I get the following error:

file$new = ifelse(file$date_var > as.Date(as.character("2015-01-01"), format = "%Y/%m/%d"), "A", "B")

Error in ... replacement has x rows, data has y
  • Is there a way I can resolve this problem?

Thank you!

Note: I am also exploring the "lubridate" library, e.g.

library(lubridate)

file$date_var = ymd(as.character(file$date_var))

file$new = ifelse(file$date_var > ymd(as.character("2015-01-01")), "A", "B")

But this is also not working...

0 Answers
Related