Home > OS >  How To Remove NA Values Dependent On A Condition in R?
How To Remove NA Values Dependent On A Condition in R?

Time:03-25

Let's say I have this data frame. How would I go about removing only the NA values associated with name a without physically removing them manually?

a     1     4
a     7     3
a     NA    4
a     6     3
a     NA    4
a     NA    3
a     2     4
a     NA    3
a     1     4
b     NA    2
c     3     NA

I've tried using the function !is.na, but that removes all the NA values in the column ID1 for all the names. How would I specifically target the ones that are associated with name a?

CodePudding user response:

You could subset your data frame as follows:

df_new <- df[!(df$name == "a" & is.na(df$ID1)), ]

This can also be written as:

df_new <- df[df$name != "a" | !is.na(df$ID1), ]

CodePudding user response:

With dplyr:

library(dplyr)

df %>%
  filter(!(name == "a" & is.na(ID1)))

Or with subset:

subset(df, !(name == "a" & is.na(ID1)))

Output

  name ID1 ID2
1    a   1   4
2    a   7   3
3    a   6   3
4    a   2   4
5    a   1   4
6    b  NA   2
7    c   3  NA

Data

df <- structure(list(name = c("a", "a", "a", "a", "a", "a", "a", "a", 
"a", "b", "c"), ID1 = c(1L, 7L, NA, 6L, NA, NA, 2L, NA, 1L, NA, 
3L), ID2 = c(4L, 3L, 4L, 3L, 4L, 3L, 4L, 3L, 4L, 2L, NA)), class = "data.frame", row.names = c(NA, 
-11L))
  •  Tags:  
  • r
  • Related