2

I have the following df where df <- data.frame(V1=c(0,0,1),V2=c(0,0,2),V3=c(-2,0,2))

If I do filter(df,rowSums!=0) I get the following error: Error in filter_impl(.data, quo) : Evaluation error: comparison (6) is possible only for atomic and list types.

Does anybody know why is that? Thanks for your help

PS: Plain rowSums(df)!=0 works just fine and gives me the expected logical

pogibas
  • 27,303
  • 19
  • 84
  • 117
David R
  • 77
  • 8
  • 1. Your are trying to compare a function, `rowSums()`, to a numeric, `0`. As the error message says, comparison is only possible for atomic and list types. 2. The `dplyr::filter` function is designed to take _column names_ as the 2nd argument. – Curt F. Feb 01 '18 at 15:08
  • Thanks Curt. Yes, I thought about that and I tried filter(df,everything(),rowSums!=0), but I get another error (Evaluation error: No tidyselect variables were registered). Is not there anyway to select all columns in cases like this? Thanks. – David R Feb 01 '18 at 16:09

2 Answers2

2

A more tidyverse style approach to the problem is to make your data tidy, i.e., with only one data value.

Sample data

my_mat <- matrix(sample(c(1, 0), replace=T, 60), nrow=30) %>% as.data.frame

Tidy data and form implicit row sums using group_by

my_mat %>% 
    mutate(row = row_number()) %>%
    gather(col, val, -row) %>%
    group_by(row) %>%
    filter(sum(val) == 0)

This tidy approach is not always as fast as base R, and it isn't always appropriate for all data types.

Curt F.
  • 4,690
  • 2
  • 22
  • 39
0

OK, I got it. filter(df,rowSums(df)!=0)

Not the most difficult one... Thanks.

David R
  • 77
  • 8