0

I am trying to remove rows that have NA values in both Column 7 and Column 12.

I have found the complete.cases command for continuous features here:

dataframe[complete.cases(dataframe[ , 5:6]),]

I have tried the following variations and got the associated errors:

dataframe[complete.cases(dataframe[ , 7,12]),]
invalid 'type' (list) of argument

dataframe[complete.cases(dataframe[ , c(7,12)]),]
Error in C(7, 12) : object not interpretable as a factor

dataframe[complete.cases(dataframe[ , 7 & 12]),]
invalid 'type' (list) of argument

I'm pretty sure I'm just messing up some basic syntax but I'm a bit new to this and drawing a blank. Key aspect, is that it needs to have NA in both column; e.g., if it has data in 7, but NA in 12 or vica versa, I want to keep it.

tchoup
  • 971
  • 4
  • 11

1 Answers1

0

You can use the following solution:

library(dplyr)

df %>% filter(!if_all(everything(), ~ is.na(.x)))

  x  y
1 1 NA
2 2  2
3 4  5

Data

structure(list(x = c(1, 2, NA, 4), y = c(NA, 2, NA, 5)), class = "data.frame", row.names = c(NA, 
-4L))

You might as well assign your desired columns to .cols argument. Here in my sample example I chose everything().

Anoushiravan R
  • 21,622
  • 3
  • 18
  • 41