Home > Back-end >  How to select columns whose elements are all 0 in R?
How to select columns whose elements are all 0 in R?

Time:04-16

I am struggling to select all columns whose elements are all 0. Here is a simple example:

df <- data.frame(A=c(1,0,1,0),B=c(0,0,0,0),C=c(1,1,0,0),D=c(0,0,0,0))
df
  A B C D
1 1 0 1 0
2 0 0 1 0
3 1 0 0 0
4 0 0 0 0

I want to select Columns B and D. I know the dplyr::selectallows to select by colnames. But how to select columns by their values? Something like select(across(everything(),~.x==0)). But the across() cannot be used in select().

CodePudding user response:

We can use where inside the select statement to find the columns that have a sum of 0.

library(dplyr)

df %>% 
  select(where(~sum(.) == 0))

Or you could use colSums in base R:

df[,colSums(df) == 0]

Output

  B D
1 0 0
2 0 0
3 0 0
4 0 0
  • Related