[Rd] apply on logical data frame together with all (PR#6560)

Duncan Murdoch dmurdoch at pair.com
Mon Feb 9 18:58:10 MET 2004


On Mon,  9 Feb 2004 18:13:29 +0100 (CET), roland.puntaier at 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



More information about the R-devel mailing list