1

I have a dataset with dates.

Class(dataset) returns "factor" Now I want to transform it into a dataset with dates. I use the as.Date function:

as.Date(dataset,  format = "%Y/%m/%d")

Now things gets weird. My data disappear. The dataset now contains NA values instead of dates

Look at this example:

eee<- c("2005-12-12", "2006-12-12", "2007-12-12")
eee
# [1] "2005-12-12" "2006-12-12" "2007-12-12"
class(eee)
# [1] "character"
fff<-as.Date(eee,  format = "%Y/%m/%d")
fff
# [1] NA NA NA

class(fff)
# [1] "Date"
Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126

1 Answers1

0

The format in as.Date() should match the format in character to be transformed e.g.

eee <- c("2005-12-12", "2006-12-12", "2007-12-12")
eee_date <-as.Date(as.character(eee),  format = "%Y-%m-%d")
class(eee_date)

Afterwards you can change the format by format() to your desired style.

eee_date <- format(eee_date, "%Y/%m/%d")
Richard Erickson
  • 2,568
  • 8
  • 26
  • 39