How to select columns in R without missing values?

R ProgrammingServer Side ProgrammingProgramming

There are two easy methods to select columns of an R data frame without missing values, first one results in a vector and other returns a matrix. For example, if we have a data frame called df then the first method can be used as df[,colSums(is.na(df))==0] and the second method will be used as t(na.omit(t(df))).

Example

Consider the below data frame −

 Live Demo

df1<-data.frame(x1=sample(c(NA,5),20,replace=TRUE),x2=rpois(20,5))
df1

Output

   x1   x2
1  NA  10
2   5   3
3  NA   6
4  NA   9
5  NA   2
6   5   1
7  NA   3
8  NA   4
9  NA   4
10  5   0
11 NA   4
12 NA   1
13  5   7
14  5   6
15 NA   4
16  5   7
17  5   8
18 NA   1
19 NA   5
20  5   5

Selecting columns of df1 that do not contain missing values using first method −

Example

df1[ , colSums(is.na(df1)) == 0]

Output

[1] 10 3 6 9 2 1 3 4 4 0 4 1 7 6 4 7 8 1 5 5

Selecting columns of df1 that do not contain missing values using second method −

Example

t(na.omit(t(df1)))

Output

       x2
[1,]  10
[2,]   3
[3,]   6
[4,]   9
[5,]   2
[6,]   1
[7,]   3
[8,]   4
[9,]   4
[10,]  0
[11,]  4
[12,]  1
[13,]  7
[14,]  6
[15,]  4
[16,]  7
[17,]  8
[18,]  1
[19,]  5
[20,]  5


attr(,"na.action")
x1
1
attr(,"class")
[1] "omit"

Example

 Live Demo

df2<-data.frame(y1=sample(c(NA,2),20,replace=TRUE),y2=rnorm(20,5,0.23))
df2

Output

    y1     y2
1  NA   4.926885
2  NA   4.880143
3   2   4.999657
4   2   5.101575
5  NA   4.778465
6  NA   4.534625
7  NA   5.341219
8   2   5.462156
9   2   4.553130
10 NA   5.282899
11 NA   4.884406
12 NA   4.960496
13  2   4.913396
14 NA   4.780874
15 NA   5.222774
16  2   5.007299
17 NA   4.900481
18  2   4.839670
19  2   5.107357
20 NA   4.874243

Selecting columns of df2 that do not contain missing values using second method −

Example

t(na.omit(t(df2)))

Output

       y2
[1,]  4.926885
[2,]  4.880143
[3,]  4.999657
[4,]  5.101575
[5,]  4.778465
[6,]  4.534625
[7,]  5.341219
[8,]  5.462156
[9,]  4.553130
[10,] 5.282899
[11,] 4.884406
[12,] 4.960496
[13,] 4.913396
[14,] 4.780874
[15,] 5.222774
[16,] 5.007299
[17,] 4.900481
[18,] 4.839670
[19,] 5.107357
[20,] 4.874243


attr(,"na.action")
y1
1
attr(,"class")
[1] "omit"
raja
Published on 16-Mar-2021 15:38:22
Advertisements