roland.puntaier@chello.at
2004-Feb-09 18:13 UTC
[Rd] apply on logical data frame together with all (PR#6560)
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)
Peter Dalgaard
2004-Feb-09 18:57 UTC
[Rd] apply on logical data frame together with all (PR#6560)
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
Duncan Murdoch
2004-Feb-09 18:58 UTC
[Rd] apply on logical data frame together with all (PR#6560)
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) > dfx 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
Possibly Parallel Threads
- apply on logical data frame together with all (PR#6560 )
- apply on logical data frame together with all (PR#6560
- how to ignore rows missing arguments of a function when creating a function?
- Plotting survival curves after multiple imputation
- reshape -> reshape 2: function cast changed?