user1489975
user1489975

Reputation: 1921

Omitting missing data in a dataframe

I have the following dataframe:

DF <- data.frame(x = c(1, 2, 3,NA), y = c(1,0, 10, NA), z=c(43,NA, 33, NA))

If I want to omit only x = NA and z = NA.

complete.cases deletes all the row contains NA for desired column.

Therefore, I am not sure how to only delete the last row in the dataframe DF.

Upvotes: 1

Views: 367

Answers (1)

Chase
Chase

Reputation: 69151

Not clear whether you want to exclude rows where x OR z = NA or x AND z = NA. Change the boolean from and & to or | if that's the case:

> DF[!(is.na(DF$x) & is.na(DF$z)),]
  x  y  z
1 1  1 43
2 2  0 NA
3 3 10 33

Upvotes: 1

Related Questions