My data called "dat":
A B C
NA 2 NA
1 2 3
1 NA 3
1 2 3
I want to be all rows to be removed if it has an NA in column B:
A B C
NA 2 NA
1 2 3
1 2 3
na.omit(dat)
removes all rows with an NA
not just the ones where the NA is in column B.
Also I'd like to know how to this for NA value in two columns.
I appreciate all advice!
To drop rows with NA's in some specific columns, you can use the filter() function from the dplyr package and the in.na() function. First, the latter one determines if a value in a column is missing and returns a TRUE or FALSE. Next, the filter function drops all rows with an NA.
To remove all rows having NA, we can use na. omit function. For Example, if we have a data frame called df that contains some NA values then we can remove all rows that contains at least one NA by using the command na. omit(df).
Select "Blanks" and click OK. Excel has now selected all of the blank cells in the column. Now carefully right-mouse click on one of the empty cells, and choose Delete from the menu. Then select Entire row, and click the OK button.
For example, we can use the subset() function if we want to drop a row based on a condition. If we prefer to work with the Tidyverse package, we can use the filter() function to remove (or select) rows based on values in a column (conditionally, that is, and the same as using subset).
The easiest solution is to use is.na()
:
df[!is.na(df$B), ]
which gives you:
A B C
1 NA 2 NA
2 1 2 3
4 1 2 3
there is an elegant solution if you use the tidyverse
!
it contains the library tidyr
that provides the method drop_na
which is very intuitive to read.
So you just do:
library(tidyverse)
dat %>% drop_na("B")
OR
dat %>% drop_na(B)
if B is a column name
try this:
df<-data.frame(A=c(NA,1,1,1),B=c(2,2,NA,2),C=c(NA,3,3,3))
df<-df[-which(is.na(df$B)),]
df
A B C
1 NA 2 NA
2 1 2 3
4 1 2 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With