Forge
Forge

Reputation: 1677

seek any true in a column from a list of data frames in R

I have a list of dataframes containing only boolean (TRUE / FALSE) columns.

I need to know if any of these columns contains any TRUE value (all are expected to be false and in fact, all are FALSE), but I'm getting just nonsense.

mylist <-
  list(df1=data.frame(
    "column11" = c(FALSE, FALSE, FALSE, FALSE),
    "column12" = c(FALSE, FALSE, FALSE, FALSE)
  ),df2=data.frame(
    "column21" = c(FALSE, FALSE, FALSE, FALSE),
    "column22" = c(FALSE, FALSE, FALSE, FALSE)
  ),df3=data.frame(
    "column31" = c(FALSE, FALSE, FALSE, FALSE),
    "column32" = c(FALSE, FALSE, FALSE, TRUE)
      ))

lapply(mylist, function(x) any(TRUE))


$df1
[1] TRUE

$df2
[1] TRUE

$df3
[1] TRUE

Upvotes: 2

Views: 80

Answers (4)

akrun
akrun

Reputation: 887078

According to ?any

Given a set of logical vectors, is at least one of the values true?

A matrix is a vector with dim attributes, but a data.frame is not. One option is to convert to matrix and use any

lapply(mylist, function(x) any(as.matrix(x)))

Upvotes: 0

BetterCallMe
BetterCallMe

Reputation: 768

We use lapply() for each dataframe in mylistthen we use apply for each column of dataframes.

abc = function(x) {
  tf = apply(x, 2, FUN = any)
}

lapply(mylist, FUN =  abc )

Output:

$df1
column11 column12 
   FALSE    FALSE 

$df2
column21 column22 
   FALSE    FALSE 

$df3
column31 column32 
   FALSE     TRUE 

Upvotes: 0

Cole
Cole

Reputation: 11255

An elusive rapply option:

rapply(mylist, f = any, how = 'unlist')

df1.column11 df1.column12 df2.column21 df2.column22 df3.column31 df3.column32 
       FALSE        FALSE        FALSE        FALSE        FALSE         TRUE 

do.call(rbind, rapply(mylist, f = any, how = 'list'))

    column11 column12
df1 FALSE    FALSE   
df2 FALSE    FALSE   
df3 FALSE    TRUE  

And with coercion:

sapply(rapply(mylist, f = any, how = 'list'), any)

  df1   df2   df3 
FALSE FALSE  TRUE 

Upvotes: 1

lroha
lroha

Reputation: 34416

One reason the anonymous function doesn't work as expected is because its argument is not being used, instead any() is checking if the value TRUE is TRUE. It also needs to be passed a logical vector, not a data.frame.

If you want to check if any column in the data.frame has a TRUE value use:

sapply(mylist, function(x) any(unlist(x)))

  df1   df2   df3 
FALSE FALSE  TRUE 

If you want to check if any particular column has a TRUE value, use:

sapply(mylist, sapply, any)

           df1   df2   df3
column11 FALSE FALSE FALSE
column12 FALSE FALSE  TRUE

Upvotes: 3

Related Questions