How to convert NaN to NA in an R data frame?


To convert NaN to NA in an R data frame, we can set the NaN values to NA values by using single square brackets. Firstly, we would need to access the column that contains NaN values then NaN values will be accessed using is.nan then we can set those values to NA as shown in the below examples.

Consider the below data frame −

Example

 Live Demo

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

Output

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

Replacing NaN with NA in column x1 of df1 −

Example

df1$x1[is.nan(df1$x1)]<-NA
df1

Output

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

Example

 Live Demo

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

Output

       y1         y2          y3
1  -0.0867355  -1.28729227   -0.20581922
2  NaN          1.11408057    0.56750161
3  NaN          0.47302790   -0.72410253
4  NaN         -0.34217866    0.33855658
5  NaN         -2.52516598    0.78809964
6  -0.0867355  -0.65268724   -0.04150251
7  -0.0867355   0.42845639   -0.12955272
8  -0.0867355   0.71995182    0.24011099
9  -0.0867355   0.07829339    3.10334568
10 -0.9135175   0.07250105   -0.41307585
11 -0.0867355   1.58477752    0.96751855
12 NaN         -1.84389126   -0.42843257
13 -0.0867355  -0.10860452   -0.32978319
14 -0.9135175  -1.25311587   -1.07537515
15 -0.0867355   0.58514141    2.05516953
16 NaN          2.02405825    0.18098639
17 -0.9135175   0.90327161   -0.22561346
18 -0.9135175   0.81260538   -1.98065156
19 NaN         -1.60608848   -1.39970818
20 NaN         -0.82239927   -1.45993348

Replacing NaN with NA in column y1 of df2 −

Example

df2$y1[is.nan(df2$y1)]<-NA
df2

Output

        y1          y2           y3
1   -0.0867355  -1.28729227   -0.20581922
2  NA            1.11408057    0.56750161
3  NA            0.47302790   -0.72410253
4  NA           -0.34217866    0.33855658
5  NA           -2.52516598    0.78809964
6  -0.0867355   -0.65268724   -0.04150251
7  -0.0867355    0.42845639   -0.12955272
8  -0.0867355    0.71995182    0.24011099
9  -0.0867355    0.07829339    3.10334568
10 -0.9135175    0.07250105   -0.41307585
11 -0.0867355    1.58477752    0.96751855
12 NA           -1.84389126   -0.42843257
13 -0.0867355   -0.10860452   -0.32978319
14 -0.9135175   -1.25311587   -1.07537515
15 -0.0867355    0.58514141    2.05516953
16 NA            2.02405825    0.18098639
17 -0.9135175    0.90327161   -0.22561346
18 -0.9135175    0.81260538   -1.98065156
19 NA           -1.60608848   -1.39970818
20 NA           -0.82239927   -1.45993348

Updated on: 10-Feb-2021

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements