0

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'.

giordano
  • 2,954
  • 7
  • 35
  • 57
  • 1
    `ifelse` basically does this: `x <- c(TRUE, FALSE); x[x] <- as.Date("2015-01-09")`. Attributes are lost during assignment to a subset even though the mode of `x` changes to `integer` which is the internal representation of a `Date` variable. – Roland Jan 09 '15 at 08:55
  • @Roland Thanks a lot for this comment. That is what I wanted to know. If you put it as answer to my question I will mark it as favourite answer. – giordano Jan 09 '15 at 13:33
  • @Roland This question ask for **why** and not for **how** to solve the problem. That's why I think that this is not a duplicate question. – giordano Jan 09 '15 at 13:38
  • the "why" is answered under "value" in `?ifelse`, and roland explained why in not so many words in his comment – rawr Jan 09 '15 at 16:48
  • @rawr "Not so many" words but much easier to understand than what is written in ?ifelse. – giordano Jan 11 '15 at 17:30

0 Answers0