
Omit rows containing specific column of NA?
I want to know how to omit `NA` values in a data frame, but only in some columns I am interested in.For example, DF <- data.frame(x = c(1, 2, 3), y = c(0, 10, NA), z=c(NA, 33, 22))but I only want to omit the data where `y` is `NA`, therefore the result should be x y z 1 1 0 NA 2 2 10 33`na.omit` seems delete all rows contain any `NA`.Can somebody help me out of this simple question?But if now I change the question like: 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` or `z=na`, where can I put the `|` in function?
More
1 Expert Answer

Ethan C. answered 12/16/24
Tutor
New to Wyzant
Experienced college math and statistics tutor
There are many ways to delete rows that contain a certain value (including NA).
In my work, I usually want to know which rows contain these values so I will write it as the following:
idx <- which(df$y == NA)
df.new <- df[-idx,]
##The minus sign (-) means to exclude in a df or matrix. For example, if I said df[-1,], this would print a df with every row except the first.
Still looking for help? Get the right answer, fast.
Ask a question for free
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Find an Online Tutor Now
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Brennan H.
09/12/19