Is the following expected behaviour for a date used in an ifelse function?> date <- Sys.Date() > date[1] "2007-12-30"> ifelse(TRUE, date-1, date)[1] 13876> ifelse(FALSE, date-1, date)[1] 13877> ifelse(TRUE, as.character(date-1), date)[1] "2007-12-29"> if (TRUE) {date}[1] "2007-12-30" It would seem more natural to me if a date produced the same format in an if and an ifelse function. Moreover, as far as I can see the ifelse function consists of hardly anything but two if functions. Mikkel> sessionInfo()R version 2.6.1 (2007-11-26) i386-pc-mingw32 locale: LC_COLLATE=English_Ireland.1252;LC_CTYPE=English_Ireland.1252;LC_MONETARY=English_Ireland.1252;LC_NUMERIC=C;LC_TIME=English_Ireland.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base ____________________________________________________________________________________ Be a better friend, newshound, and
Read the warning in ?ifelse On Dec 30, 2007 1:45 AM, Mikkel Grum <mi2kelgrum at yahoo.com> wrote:> Is the following expected behaviour for a date used in > an ifelse function? > > > date <- Sys.Date() > > date > [1] "2007-12-30" > > ifelse(TRUE, date-1, date) > [1] 13876 > > ifelse(FALSE, date-1, date) > [1] 13877 > > ifelse(TRUE, as.character(date-1), date) > [1] "2007-12-29" > > if (TRUE) {date} > [1] "2007-12-30" > > It would seem more natural to me if a date produced > the same format in an if and an ifelse function. > Moreover, as far as I can see the ifelse function > consists of hardly anything but two if functions. > > Mikkel > > > sessionInfo() > R version 2.6.1 (2007-11-26) > i386-pc-mingw32 > > locale: > LC_COLLATE=English_Ireland.1252;LC_CTYPE=English_Ireland.1252;LC_MONETARY=English_Ireland.1252;LC_NUMERIC=C;LC_TIME=English_Ireland.1252 > > attached base packages: > [1] stats graphics grDevices utils datasets > methods base > > > > ____________________________________________________________________________________ > Be a better friend, newshound, and > > ______________________________________________ > 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. >
?ifelse will give you Warning The mode of the result may depend on the value of test, and the class attribute of the result is taken from test and may be inappropriate for the values selected from yes and no. i think it might answer your question, Weiwei On Dec 30, 2007 1:45 AM, Mikkel Grum <mi2kelgrum@yahoo.com> wrote:> Is the following expected behaviour for a date used in > an ifelse function? > > > date <- Sys.Date() > > date > [1] "2007-12-30" > > ifelse(TRUE, date-1, date) > [1] 13876 > > ifelse(FALSE, date-1, date) > [1] 13877 > > ifelse(TRUE, as.character(date-1), date) > [1] "2007-12-29" > > if (TRUE) {date} > [1] "2007-12-30" > > It would seem more natural to me if a date produced > the same format in an if and an ifelse function. > Moreover, as far as I can see the ifelse function > consists of hardly anything but two if functions. > > Mikkel > > > sessionInfo() > R version 2.6.1 (2007-11-26) > i386-pc-mingw32 > > locale: > > LC_COLLATE=English_Ireland.1252;LC_CTYPE=English_Ireland.1252;LC_MONETARY=English_Ireland.1252;LC_NUMERIC=C;LC_TIME=English_Ireland.1252 > > attached base packages: > [1] stats graphics grDevices utils datasets > methods base > > > > > ____________________________________________________________________________________ > Be a better friend, newshound, and > > ______________________________________________ > R-help@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. >-- Weiwei Shi, Ph.D Research Scientist GeneGO, Inc. "Did you always know?" "No, I did not. But I believed..." ---Matrix III [[alternative HTML version deleted]]
Thanks to both of you. I tried to work on it, but the closest I could get was: lifelse <- function (test, y, z) { iffy <- function(x) { if (x) {y} else {z} } lapply(test, iffy) }> lifelse(c(TRUE, FALSE), date -1, date)[[1]] [1] "2007-12-29" [[2]] [1] "2007-12-30"> > d <- as.Date(c("1994-3-4", "1996-3-1")) > lifelse(d > "1996-1-1", "1996-1-1", d)[[1]] [1] "1994-03-04" "1996-03-01" [[2]] [1] "1996-1-1"> lifelse(d <= "1996-1-1", d, "1996-1-1")[[1]] [1] "1994-03-04" "1996-03-01" [[2]] [1] "1996-1-1" Any attempts to unlist, paste, etc. to remove the list structure converted/removed the Date class. Mikkel ----- Original Message ---- From: Peter Dalgaard <p.dalgaard at biostat.ku.dk> To: Gabor Grothendieck <ggrothendieck at gmail.com> Cc: Mikkel Grum <mi2kelgrum at yahoo.com>; r-help at stat.math.ethz.ch Sent: Sunday, December 30, 2007 1:47:26 PM Subject: Re: [R] Date formats Gabor Grothendieck wrote:> Read the warning in ?ifelseYep. And, yes, it is annoying that ifelse() strips attributes, including class, but it is one of those things that have been in the S languages "forever", and nobody really wants to mess with. The fundamental issue is that you need the result to be able to hold values from both of the "yes" and the "no" arguments and there is no guarantee that that is possible outside of the R base types. You'd like to have things like these "work" d <- as.Date(c("1994-3-4", "1996-3-1")) ifelse(d > "1996-1-1", "1996-1-1", d) ifelse(d <= "1996-1-1", d, "1996-1-1") in the sense that the result is a Date object, but once you start thinking about the details of how it _might_ work, you find that things aren't all that simple. If there was a general mechanism for coercion between classes, then maybe it could be done, but there isn't any. -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 ____________________________________________________________________________________ Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ