Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove row if it has a NA value in one certain column [duplicate]

Tags:

r

na

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!

like image 666
user9259005 Avatar asked Feb 07 '18 08:02

user9259005


People also ask

How do I delete a row with Na in a specific column?

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.

How do I remove rows containing 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).

How do I delete rows in Excel with Na value?

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.

How do I remove a row based on a condition in R?

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).


3 Answers

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
like image 70
clemens Avatar answered Oct 09 '22 20:10

clemens


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

like image 42
Agile Bean Avatar answered Oct 09 '22 20:10

Agile Bean


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
like image 6
jyjek Avatar answered Oct 09 '22 21:10

jyjek