0

I have the following data frame structure:

x1   x2   x3  x4  
1    NA   NA  NA     
2    1    NA  3       
3    4    5   6       
4    7    8   NA      
5    NA   NA  NA 

I would like to remove the rows if all x2->x4 are NA's (in my data set I have 28 columns, named "1" to "28", and the first one is never NA). In other words, I would like to keep:

x1   x2   x3  x4      
2    1    NA  3       
3    4    5   6       
4    7    8   NA 

Any ideas?

Thanks in advance !

2 Answers2

1
df[rowSums(is.na(df[,2:4]))!=3,]
  x1 x2 x3 x4
2  2  1 NA  3
3  3  4  5  6
4  4  7  8 NA
user2974951
  • 9,535
  • 1
  • 17
  • 24
0

You can use filter_at :

library(dplyr)
df %>% filter_at(vars(x2:x4), any_vars(!is.na(.)))

#  x1 x2 x3 x4
#2  2  1 NA  3
#3  3  4  5  6
#4  4  7  8 NA
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213