1

I got this dataframe:

df <- data.frame(
    a = c("1", "2", "", "")
    , b = c("1", "", "3", "")
)

  a b
1 1 1
2 2  
3   3
4    

I want to indentify (and then actually remove) the rows where all columns fulfill one particular criterion - in this case == ""

Desired output:

  a b
1 1 1
2 2  
3   3
Georgery
  • 7,643
  • 1
  • 19
  • 52

2 Answers2

1

We can create a logical matrix by doing the comparison with ==, get the rowSums of the TRUE values, check if it is not equal to number of columns of dataset to subset the rows

df[rowSums(df == "") != ncol(df),]
#  a b
#1 1 1
#2 2  
#3   3

Or another option is filter_all

library(dplyr)
df %>%
   filter_all(any_vars(. != ''))
#   a b
#1 1 1
#2 2  
#3   3
akrun
  • 874,273
  • 37
  • 540
  • 662
1

Another option with base R would be:

df[sapply(1: nrow(df), function (i) all(df[i,] == '')) != TRUE, ]

#   a b
# 1 1 1
# 2 2  
# 3   3
AlexB
  • 3,061
  • 2
  • 17
  • 19