How to remove rows from an R data frame that contains at least one NaN?


The NA values and NaN values are very different in nature, therefore, removal of rows containing NA values is different from removal of rows containing NaN values. For example, if we have a data frame that has NaN values the rows will be removed by using the is.finite function as shown in the below examples.

Consider the below data frame −

Example

 Live Demo

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

Output

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

Removing rows containing NaN values from df1 −

df1[is.finite(df1$x1) & is.finite(df1$x2),]

   x1 x2
1  2  2
2  2  3
3  0  6
4  2  3
5  2  2
7  2  3
9  2  2
11 2  2
13 8  6
18 8  2

Example

 Live Demo

y1<-sample(c(NaN,rnorm(2)),20,replace=TRUE)
y2<-sample(c(NaN,rnorm(2)),20,replace=TRUE)
y3<-sample(c(NaN,rnorm(2)),20,replace=TRUE)
df2<-data.frame(y1,y2,y3)
df2

Output

   y1             y2           y3
1  NaN          0.3324506    -0.5461648
2  0.3319275   -0.3103766    -0.5461648
3  1.5624962    0.3324506    -0.3322423
4  NaN         -0.3103766    -0.3322423
5  1.5624962    NaN           NaN
6  NaN         -0.3103766    -0.3322423
7  1.5624962    0.3324506     NaN
8  0.3319275   -0.3103766    -0.3322423
9  NaN         -0.3103766    -0.3322423
10 1.5624962    NaN           NaN
11 0.3319275    NaN           NaN
12 1.5624962    0.3324506    -0.3322423
13 1.5624962    NaN           NaN
14 NaN          0.3324506     NaN
15 1.5624962    0.3324506    -0.5461648
16 1.5624962   -0.3103766    -0.3322423
17 NaN          0.3324506    -0.5461648
18 NaN          0.3324506    -0.3322423
19 0.3319275   -0.3103766    -0.5461648
20 0.3319275    NaN          -0.3322423

Removing rows containing NaN values from df2 −

df2[is.finite(df2$y1) & is.finite(df2$y2) & is.finite(df2$y3),]

      y1         y2          y3
2  0.3319275   -0.3103766   -0.5461648
3  1.5624962    0.3324506   -0.3322423
8  0.3319275   -0.3103766   -0.3322423
12 1.5624962    0.3324506   -0.3322423
15 1.5624962    0.3324506   -0.5461648
16 1.5624962   -0.3103766   -0.3322423
19 0.3319275   -0.3103766   -0.5461648

Updated on: 08-Feb-2021

239 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements