[R] converting multiple columns from POSIX* to Date

Marc Schwartz marc_schwartz at comcast.net
Thu Dec 11 00:47:06 CET 2008


on 12/10/2008 05:26 PM Farrel Buchinsky wrote:
> converting a POSIX class variable to a date class is easy.
> dates<-as.Date(x) #where X is of class POSIX
> How does one do that to all columns in a data frame that are of POSIX
> class and leave all the other columns (integers, factors) as is.
> 
> Feel free to reply with just one or two buzzwords that I could then
> search for to find how to do it.
> 
> Farrel Buchinsky

If 'DF' is your source data frame, something like:

  DF[] <- lapply(DF, function(x) ifelse(class(x) == "POSIXt",
                                        as.Date(x), x)


This is untested, so proceed with due caution.

HTH,

Marc Schwartz



More information about the R-help mailing list