I have imported a CSV file with numerous dates and have use the as.Date function to convert the date. However, when I use the the mapply function to find the earlier of two dates, I either end up with a list with dates or a numeric vector. How can I get a vector with dates?
POP.Start.final <- mapply(min, combinedOM$Cons.Start.Date.y,
combinedOM$OS.Start.Date.y,
MoreArgs = list(na.rm=T),SIMPLIFY=T)
This returns a numeric vector, changing to SIMPLIFY=F returns a list of dates, but I want a vector of dates.
In this specific case, I believe
pmindoes the trick:In the general case,
SIMPLIFY=TRUE(the default) uses the utility functionsimplify2arrayto convert lists to vectors of atomic mode viaas.vector. Because dates are internally stored as numeric,SIMPLIFY=TRUEwill convert the list of dates to a vector of mode numeric and remove theDateclass. You can setSIMPLIFY=FALSEto keep theDateclass and then usedo.callwithcto convert the list to a vector.Some reproducible code:
As an aside, using
TandFforTRUEandFALSEis slightly worrying becauseTandFcan be reassigned whileTRUEandFALSEcannot be reassigned.