How to subset rows that do not contain NA and blank in one of the columns in an R data frame?

R ProgrammingServer Side ProgrammingProgramming

It is possible that we get data sets where a column contains NA as well as blank, therefore, it becomes necessary to deal with these values. One of the ways to deal with these values is selecting the rows where we do not have them. This can be done by subsetting through single square brackets.

Example

Consider the below data frame −

> set.seed(1)
> x1<-sample(1:50,20)
> x2<-rep(c(1,"",3,4),times=5)
> x3<-rep(c(5,NA,10,"",20),each=4)
> df<-data.frame(x1,x2,x3)
> df
  x1 x2 x3
1 4 1 5
2 39 5
3 1 3 5
4 34 4 5
5 23 1 <NA>
6 43 <NA>
7 14 3 <NA>
8 18 4 <NA>
9 33 1 10
10 21 10
11 41 3 10
12 10 4 10
13 7 1
14 9
15 15 3
16 40 4
17 25 1 20
18 47 20
19 12 3 20
20 36 4 20

Subsetting rows where x3 is neither NA nor blank −

> df[!(is.na(df$x3) | df$x3==""), ]
x1 x2 x3
1 4 1 5
2 39 5
3 1 3 5
4 34 4 5
9 33 1 10
10 21 10
11 41 3 10
12 10 4 10
17 25 1 20
18 47 20
19 12 3 20
20 36 4 20

Subsetting rows where x2 is not blank −

> df[!(df$x2==""), ]
x1 x2 x3
1 4 1 5
3 1 3 5
4 34 4 5
5 23 1 <NA>
7 14 3 <NA>
8 18 4 <NA>
9 33 1 10
11 41 3 10
12 10 4 10
13 7 1
15 15 3
16 40 4
17 25 1 20
19 12 3 20
20 36 4 20
raja
Published on 12-Aug-2020 13:32:17
Advertisements