Full_Name: Roland Puntaier Version: 1.8.1 OS: Windows XP Submission from: (NULL) (62.99.238.78) I have a data frame with some columns being logical. I wanted to calculate a column that is an AND combination of the other logical columns. I tried to use apply(df,1,all) It did not work because of the implicit string conversion. So I made the functions All<-function(...) all(as.logical(...))#because all cannot be used with apply Any<-function(...) any(as.logical(...))#because any cannot be used with apply Now apply(df,1,All) seemed to work, but produced some NAs where there was no reason for them. I debugged apply and found that some logical values are transformed to " TRUE" instead of "TRUE". I did not follow that up closer. Instead I worked around it by doing the string conversion myself and then call apply(dfc,1,All)
apply on logical data frame together with all (PR#6560)
3 messages · roland.puntaier@chello.at, Peter Dalgaard, Duncan Murdoch
roland.puntaier@chello.at writes:
Full_Name: Roland Puntaier Version: 1.8.1 OS: Windows XP Submission from: (NULL) (62.99.238.78) I have a data frame with some columns being logical. I wanted to calculate a column that is an AND combination of the other logical columns. I tried to use apply(df,1,all) It did not work because of the implicit string conversion. So I made the functions All<-function(...) all(as.logical(...))#because all cannot be used with apply Any<-function(...) any(as.logical(...))#because any cannot be used with apply Now apply(df,1,All) seemed to work, but produced some NAs where there was no reason for them. I debugged apply and found that some logical values are transformed to " TRUE" instead of "TRUE". I did not follow that up closer. Instead I worked around it by doing the string conversion myself and then call apply(dfc,1,All)
It's as documented (i.e. no bug) and the five-letter field for TRUE is
consequence of using format().
Behaviour has been changed in r-devel though, since you weren't the
first to be annoyed by it....
Meanwhile, I believe that do.call("cbind",df) is a better workaround.
O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk) FAX: (+45) 35327907
On Mon, 9 Feb 2004 18:13:29 +0100 (CET), roland.puntaier@chello.at wrote :
Full_Name: Roland Puntaier Version: 1.8.1 OS: Windows XP Submission from: (NULL) (62.99.238.78) I have a data frame with some columns being logical. I wanted to calculate a column that is an AND combination of the other logical columns. I tried to use apply(df,1,all) It did not work because of the implicit string conversion. So I made the functions All<-function(...) all(as.logical(...))#because all cannot be used with apply Any<-function(...) any(as.logical(...))#because any cannot be used with apply Now apply(df,1,All) seemed to work, but produced some NAs where there was no reason for them.
Here's a reproducible example of this:
x <- c(FALSE, TRUE) y <- x df <- data.frame(x,y) df
x y 1 FALSE FALSE 2 TRUE TRUE
apply(df, 1, all)
Error in all(..., na.rm = na.rm) : incorrect argument type
All <- function(...) all(as.logical(...)) apply(df, 1, All)
1 2
FALSE NA
The good news is that r-devel (to become 1.9.0) gives the correct
result because of this change:
o as.matrix.data.frame() now coerces an all-logical data frame
to a logical matrix.
The bad news is that r-patched does not. I guess the workaround while
you're waiting for 1.9 would be to write All as follows:
All<-function(x) all(as.logical(gsub(" ", "", x)))
Thanks for the report.
Duncan Murdoch