[R] preserving date formats in functions
Uwe Ligges
ligges at statistik.tu-dortmund.de
Sat Jun 9 17:21:51 CEST 2012
On 05.06.2012 21:44, jween wrote:
> Hi there!
>
> I have two date columns in a dataframe I need to selectively collapse based
> on missing values and which date comes first (imported from an text file).
> This is what I did:
>
> RHSSP$CT <- as.POSIXct(RHSSP$CT, format='%m/%d/%y %H:%M')
> RHSSP$MRI <- as.POSIXct(RHSSP$MRI, format='%m/%d/%y %H:%M')
> RHSSP$Scan <-
> ifelse(is.na(RHSSP$MRI),RHSSP$CT,ifelse(is.na(RHSSP$CT),RHSSP$MRI,ifelse(RHSSP$CT>RHSSP$MRI,RHSSP$MRI,RHSSP$CT)))
>
> RHSSP$CT and RHSSP$MRI remain properly formatted (e.g.: "2011-06-21 22:31:00
> PDT") BUT RHSSP$Scan does not (e.g.: 1294076700)
>
> I can't figure out how to maintain the proper format during the replacement.
> Looks like the replacement inherits the ifelse comparison format.
>
> Any suggestions?
Yes: Read ?ifelse that tells us:
===
Warning
The mode of the result may depend on the value of test (see the
examples), and the class attribute (see oldClass) of the result is taken
from test and may be inappropriate for the values selected from yes and no.
Sometimes it is better to use a construction such as (tmp <- yes;
tmp[!test] <- no[!test]; tmp), possibly extended to handle missing
values in test.
===
Best,
Uwe Ligges
>
> Many thanks
>
> Jon
>
> --
> View this message in context: http://r.789695.n4.nabble.com/preserving-date-formats-in-functions-tp4632435.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list