thentangler
thentangler

Reputation: 1256

Filtering columns where all rows match a specific value in R

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?

Upvotes: 1

Views: 313

Answers (2)

AndrewGB
AndrewGB

Reputation: 16876

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))

Upvotes: 1

Onyambu
Onyambu

Reputation: 79358

In base R you could do:

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

or even

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

Upvotes: 2

Related Questions