1
votes

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

2
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.
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 Rengel

2 Answers

1
votes

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.

0
votes

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

Not the most difficult one... Thanks.