[R] date columns chooser

peter dalgaard pdalgd at gmail.com
Wed Mar 7 19:26:55 CET 2012


On Mar 7, 2012, at 19:06 , henk harmsen wrote:

> i have a data frame with 2 columns of dates.
> with str(dataframe) i have ensured myself that they were indeed formatted
> as dates.
> one column has NA's in it.
> 
> the aim is now to make a third column that chooses date1 if it is a date,
> and choose date2 if it is a NA.
> 
> i am trying
> df$date3=ifelse(is.na(df$date1), df$date2, df$date1).
> 
> this leads to unexpected behaviour: the resulting column is numeric, and
> shows numbers like 16000.
> 
> i have no idea what this is and how to solve it?

It's an annoyance.... (See the Value section of ?ifelse for documentation, if not explanation. Basically, ifelse cannot tell from which of the two relevant arguments it should get attributes, so it takes them from the 3rd.)

The most convenient way out is to use indexing; e.g.,

df$date3 <- local({
  tmp <- df$date1
  ix <- is.na(tmp)
  tmp[ix] <- df$date2[ix]
  tmp
})

(The local() stuff is there just to get rid of the tmp and ix variables in a clean way.)

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list