Using ifelse() on a column with class Date results in a numeric column. Here is an example:
# Data
x <- data.frame(dt1 = as.Date(c('2001-01-01','2001-01-02',NA)))
x
dt1
1 2001-01-01
2 2001-01-02
3 <NA>
# Copy
x$dt2 <- x$dt1
x
dt1 dt2
1 2001-01-01 2001-01-01
2 2001-01-02 2001-01-02
3 <NA> <NA>
str(x)
'data.frame': 3 obs. of 2 variables:
$ dt1: Date, format: "2001-01-01" "2001-01-02" NA
$ dt2: Date, format: "2001-01-01" "2001-01-02" NA
Copy a column of datatype Date gives the same datatype as expected. But if I use ifelse() I got different datatypes:
x$dt3 <- with(x, ifelse(is.na(dt1),'9999-12-31',dt1))
x$dt4 <- with(x, ifelse(is.na(dt1),as.Date('9999-12-31'),dt1))
x
dt1 dt2 dt3 dt4
1 2001-01-01 2001-01-01 11323 11323
2 2001-01-02 2001-01-02 11324 11324
3 <NA> <NA> 9999-12-31 2932896
str(x)
'data.frame': 3 obs. of 4 variables:
$ dt1: Date, format: "2001-01-01" "2001-01-02" NA
$ dt2: Date, format: "2001-01-01" "2001-01-02" NA
$ dt3: chr "11323" "11324" "9999-12-31"
$ dt4: num 11323 11324 2932896
To get the expected results I have to convert the numeric version into Date:
x$dt4 <- as.Date(x$dt4,origin = "1970-01-01")
x
dt1 dt2 dt3 dt4
1 2001-01-01 2001-01-01 11323 2001-01-01
2 2001-01-02 2001-01-02 11324 2001-01-02
3 <NA> <NA> 9999-12-31 9999-12-31
I'm just wondering why this happend. It seems that when a function is applied to a column it uses the numeric value (behind the formatted value). The numeric output should then be converted manually in the desired format. If this is the case this will arise some problems due to the definition of origin. I hope that the implicit conversion of the Date to numeric by using functions (e.g. ifelse) is always referred to origin '1970-01-01'.