Hi,
I was "losing" my dates in a script and upon inspection, found that my recent switch from separate "if" and "else" to "ifelse" was the cause. But why?
my.date = as.POSIXct("2011-06-04 08:00:00")
default.date = seq(as.POSIXct("2011-01-01 08:00:00"), as.POSIXct("2011-09-01 08:00:00"), length=15)
x = 4 * 60 * 60
(my.date + x)
(min(default.date) + x)
(new.date = ifelse(!is.na(my.date), my.date + x, min(default.date) + x) )
(if(!is.na(my.date)) new.date2 = my.date + x else new.date2= min(default.date) + x )
On my machine, new.date is "numeric" whereas new.date2 is "POSIXct" and "POSIXt", as desired.
sessionInfo()
R version 2.13.0 (2011-04-13)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
locale:
[1] fr_CA.UTF-8/fr_CA.UTF-8/C/C/fr_CA.UTF-8/fr_CA.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
Thanks in advance,
Denis
ifelse strips POSIXct class from object
7 messages · Denis Chabot, Gabor Grothendieck, Duncan Murdoch
On 11-06-05 8:23 AM, Denis Chabot wrote:
Hi, I was "losing" my dates in a script and upon inspection, found that my recent switch from separate "if" and "else" to "ifelse" was the cause. But why?
See ?ifelse. The class of the result is the same as the class of the test, not the classes of the alternatives. You need to manually attach the class again, or use a different construction. Duncan Murdoch
my.date = as.POSIXct("2011-06-04 08:00:00")
default.date = seq(as.POSIXct("2011-01-01 08:00:00"), as.POSIXct("2011-09-01 08:00:00"), length=15)
x = 4 * 60 * 60
(my.date + x)
(min(default.date) + x)
(new.date = ifelse(!is.na(my.date), my.date + x, min(default.date) + x) )
(if(!is.na(my.date)) new.date2 = my.date + x else new.date2= min(default.date) + x )
On my machine, new.date is "numeric" whereas new.date2 is "POSIXct" and "POSIXt", as desired.
sessionInfo()
R version 2.13.0 (2011-04-13)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
locale:
[1] fr_CA.UTF-8/fr_CA.UTF-8/C/C/fr_CA.UTF-8/fr_CA.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
Thanks in advance,
Denis
______________________________________________ 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.
On Sun, Jun 5, 2011 at 8:23 AM, Denis Chabot <chabot.denis at gmail.com> wrote:
Hi,
I was "losing" my dates in a script and upon inspection, found that my recent switch from separate "if" and "else" to "ifelse" was the cause. But why?
my.date = as.POSIXct("2011-06-04 08:00:00")
default.date = seq(as.POSIXct("2011-01-01 08:00:00"), as.POSIXct("2011-09-01 08:00:00"), length=15)
x = 4 * 60 * 60
(my.date + x)
(min(default.date) + x)
(new.date = ifelse(!is.na(my.date), my.date + x, min(default.date) + x) )
Try replace: new.date <- replace(my.date, is.na(my.date), min(default.date)) + x
Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Thanks Duncan, I'll go back to if and else! Denis Le 2011-06-05 ? 08:39, Duncan Murdoch a ?crit :
On 11-06-05 8:23 AM, Denis Chabot wrote:
Hi, I was "losing" my dates in a script and upon inspection, found that my recent switch from separate "if" and "else" to "ifelse" was the cause. But why?
See ?ifelse. The class of the result is the same as the class of the test, not the classes of the alternatives. You need to manually attach the class again, or use a different construction. Duncan Murdoch
my.date = as.POSIXct("2011-06-04 08:00:00")
default.date = seq(as.POSIXct("2011-01-01 08:00:00"), as.POSIXct("2011-09-01 08:00:00"), length=15)
x = 4 * 60 * 60
(my.date + x)
(min(default.date) + x)
(new.date = ifelse(!is.na(my.date), my.date + x, min(default.date) + x) )
(if(!is.na(my.date)) new.date2 = my.date + x else new.date2= min(default.date) + x )
On my machine, new.date is "numeric" whereas new.date2 is "POSIXct" and "POSIXt", as desired.
sessionInfo()
R version 2.13.0 (2011-04-13)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
locale:
[1] fr_CA.UTF-8/fr_CA.UTF-8/C/C/fr_CA.UTF-8/fr_CA.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
Thanks in advance,
Denis
______________________________________________ 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.
I did not know this function, thanks a lot Gabor. Denis Le 2011-06-05 ? 08:48, Gabor Grothendieck a ?crit :
On Sun, Jun 5, 2011 at 8:23 AM, Denis Chabot <chabot.denis at gmail.com> wrote:
Hi,
I was "losing" my dates in a script and upon inspection, found that my recent switch from separate "if" and "else" to "ifelse" was the cause. But why?
my.date = as.POSIXct("2011-06-04 08:00:00")
default.date = seq(as.POSIXct("2011-01-01 08:00:00"), as.POSIXct("2011-09-01 08:00:00"), length=15)
x = 4 * 60 * 60
(my.date + x)
(min(default.date) + x)
(new.date = ifelse(!is.na(my.date), my.date + x, min(default.date) + x) )
Try replace: new.date <- replace(my.date, is.na(my.date), min(default.date)) + x -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
On 11-06-05 8:49 AM, Denis Chabot wrote:
Thanks Duncan, I'll go back to if and else!
Be careful, it might not give you the same answer. I'd use this variation on the advice from ?ifelse: new.date <- my.date + x new.date[is.na(my.date)] <- min(default.date) + x The thing to watch out for in this construction is that the lengths of the vectors come out right. I'm assuming that my.date + x is the same length as is.na(my.date)], and that min(default.date) + x is length 1, but I haven't tried your code to check. Duncan Murdoch
Denis Le 2011-06-05 ? 08:39, Duncan Murdoch a ?crit :
On 11-06-05 8:23 AM, Denis Chabot wrote:
Hi, I was "losing" my dates in a script and upon inspection, found that my recent switch from separate "if" and "else" to "ifelse" was the cause. But why?
See ?ifelse. The class of the result is the same as the class of the test, not the classes of the alternatives. You need to manually attach the class again, or use a different construction. Duncan Murdoch
my.date = as.POSIXct("2011-06-04 08:00:00")
default.date = seq(as.POSIXct("2011-01-01 08:00:00"), as.POSIXct("2011-09-01 08:00:00"), length=15)
x = 4 * 60 * 60
(my.date + x)
(min(default.date) + x)
(new.date = ifelse(!is.na(my.date), my.date + x, min(default.date) + x) )
(if(!is.na(my.date)) new.date2 = my.date + x else new.date2= min(default.date) + x )
On my machine, new.date is "numeric" whereas new.date2 is "POSIXct" and "POSIXt", as desired.
sessionInfo()
R version 2.13.0 (2011-04-13)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
locale:
[1] fr_CA.UTF-8/fr_CA.UTF-8/C/C/fr_CA.UTF-8/fr_CA.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
Thanks in advance,
Denis
______________________________________________ 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.
Hi Duncan, In this case they all had length 1, but I'll be careful at other occasions. Denis Le 2011-06-05 ? 09:26, Duncan Murdoch a ?crit :
On 11-06-05 8:49 AM, Denis Chabot wrote:
Thanks Duncan, I'll go back to if and else!
Be careful, it might not give you the same answer. I'd use this variation on the advice from ?ifelse: new.date <- my.date + x new.date[is.na(my.date)] <- min(default.date) + x The thing to watch out for in this construction is that the lengths of the vectors come out right. I'm assuming that my.date + x is the same length as is.na(my.date)], and that min(default.date) + x is length 1, but I haven't tried your code to check. Duncan Murdoch
Denis Le 2011-06-05 ? 08:39, Duncan Murdoch a ?crit :
On 11-06-05 8:23 AM, Denis Chabot wrote:
Hi, I was "losing" my dates in a script and upon inspection, found that my recent switch from separate "if" and "else" to "ifelse" was the cause. But why?
See ?ifelse. The class of the result is the same as the class of the test, not the classes of the alternatives. You need to manually attach the class again, or use a different construction. Duncan Murdoch
my.date = as.POSIXct("2011-06-04 08:00:00")
default.date = seq(as.POSIXct("2011-01-01 08:00:00"), as.POSIXct("2011-09-01 08:00:00"), length=15)
x = 4 * 60 * 60
(my.date + x)
(min(default.date) + x)
(new.date = ifelse(!is.na(my.date), my.date + x, min(default.date) + x) )
(if(!is.na(my.date)) new.date2 = my.date + x else new.date2= min(default.date) + x )
On my machine, new.date is "numeric" whereas new.date2 is "POSIXct" and "POSIXt", as desired.
sessionInfo()
R version 2.13.0 (2011-04-13)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
locale:
[1] fr_CA.UTF-8/fr_CA.UTF-8/C/C/fr_CA.UTF-8/fr_CA.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
Thanks in advance,
Denis
______________________________________________ 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.