"ifelse" function eturns numbers instead of dates

Currently I’m involved in data manipulation task in order to create a data table ready for further analysis.

Suppose in a small data frame two date columns are around:

  Treaty_Date.x Treaty_Date.y
1    2020-12-31          <NA>
2          <NA>    2019-05-22
3    2020-10-13    2019-09-01

The first one is "Treaty_Date.x", the second – "Treaty_Date.y". NA’s are also available.

Implementing a code presented below (written using basic "ifelse" function),

ResSet2$Treaty_Date <- ifelse(
    (is.na(ResSet2$Treaty_Date.x) & is.na(ResSet2$Treaty_Date.y)),
    "No data",
    ifelse(
        (is.na(ResSet2$Treaty_Date.x) & !is.na(ResSet2$Treaty_Date.y)),
        ResSet2$Treaty_Date.y,
        ifelse(
            (!is.na(ResSet2$Treaty_Date.x) &  is.na(ResSet2$Treaty_Date.y)),
            ResSet2$Treaty_Date.x,
            ifelse(
                (!is.na(ResSet2$Treaty_Date.x) & !is.na(ResSet2$Treaty_Date.y)),
                ResSet2$Treaty_Date.y,
                "No data"
            )
        )
    )
)

dates in a newly created column "Treaty_Date" become numbers (see a screenshot below). Given instructions presented for the function I do understand that it is a function’s feature that prevents me from getting correct format.

  Treaty_Date.x Treaty_Date.y Treaty_Date
1    2020-12-31          <NA>       18627
2          <NA>    2019-05-22       18038
3    2020-10-13    2019-09-01       18140

But how it is possible to overcome this "drawback" in a case where I have to use nested ifelse statement?

Data

ResSet2 <- structure(list(Treaty_Date.x = structure(c(18627, NA, 18548), class = "Date"), 
    Treaty_Date.y = structure(c(NA, 18038, 18140), class = "Date")), row.names = c(NA, 
-3L), class = "data.frame")

>Solution :

Basically, the new column should be treaty date y, if available, otherwise treaty date x, otherwise nothing. Another option

ResSet2$Treaty_Date=ResSet2$Treaty_Date.y
ResSet2$Treaty_Date[is.na(ResSet2$Treaty_Date)]=
  ResSet2$Treaty_Date.x[is.na(ResSet2$Treaty_Date)]

  Treaty_Date.x Treaty_Date.y Treaty_Date
1    2020-12-31          <NA>  2020-12-31
2          <NA>    2019-05-22  2019-05-22
3    2020-10-13    2019-09-01  2019-09-01

Leave a Reply