Home > Software design >  Filtering columns where all rows match a specific value in R
Filtering columns where all rows match a specific value in R

Time:06-21

I have a dataframe like so:

Apple   Orange  Strawberry
0           1       1
0           1       1
0           1       0
0           1       0
0           1       0
0           1       1
0           1       1

I want to filter the dataframe such that I get the column names where all the rows are a specific value say 0. In this case I would get Apple

I tried doing

df[rowSums(df<1)==0, ]

but I'm just getting an empty dataframe with all the column names. Is there something else I can try?

CodePudding user response:

In base R you could do:

names(df)[colSums(df) == 0]

or even

names(Filter(all, data.frame(df == 0)))
[1] "Apple"

CodePudding user response:

Here is another option:

library(tidyverse)

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

Output

[1] "Apple"

Data

df <- structure(list(Apple = c(0L, 0L, 0L, 0L, 0L, 0L, 0L), Orange = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L), Strawberry = c(1L, 1L, 0L, 0L, 0L, 1L, 
1L)), class = "data.frame", row.names = c(NA, -7L))
  • Related