Replace NA's with numeric values

I have a dataset that is similar to this where the first 5 value's in the calculation come up as NaN. I am looking to write a for loop that looks through the column sample$x2 and replaces any "NaN" with 0.00000000

> sample <- data.frame(x1 = 1:5,
                     x2 = "NA")
> sample

> sample
  x1 x2
1  1 NA
2  2 NA
3  3 NA
4  4 NA
5  5 NA

Hi @ImranJ,

A couple of comments before exploring some solutions. NA and NaN are different in R (and many other languages). NA is literally "not available", and usually represents missing data, while NaN represents "not a number". In lots of computations, they may act the same, but I think it is worth nothing that difference. Also worth pointing out that "NA" is actually just a character string and not true NA which is a special data type in R. For example:

is.na("NA")
#> [1] FALSE
is.na(NA)
#> [1] TRUE

Since your data are stored in a column, you just need to use a function that applies the requisite task on a column of a data frame. No for loop needed. Here are two solutions (there are many more ways to achieve the same thing in R):

sample <- data.frame(x1 = 1:5,
                     x2 = NA)

# base R
sample1 <- sample
sample1$x2 <- ifelse(is.na(sample$x2), 999, sample$x2)
sample1
#>   x1  x2
#> 1  1 999
#> 2  2 999
#> 3  3 999
#> 4  4 999
#> 5  5 999

# tidyverse way
library(tidyr)
sample2 <- replace_na(sample, list(x2 = 999))
sample2
#>   x1  x2
#> 1  1 999
#> 2  2 999
#> 3  3 999
#> 4  4 999
#> 5  5 999

Created on 2020-07-03 by the reprex package (v0.3.0)

I am not sure if your are dealing with NA or NaN values so I made an example with both. Those are not the same as the string "NA" that you used in your example. If a data frame column contains the string "NA", then all the other elements of the column will also be characters, not numbers.
I used the ifelse function which is a vectorized version of if. It walks down the column testing if is.na() is TRUE. When it is TRUE it replaces the NA with zero, otherwise is replaces the value with the current value, resulting in no change.

DF <- data.frame(x1 = 1:5,
                     x2 = c(11, 12, NA, 4, NaN))
DF
#>   x1  x2
#> 1  1  11
#> 2  2  12
#> 3  3  NA
#> 4  4   4
#> 5  5 NaN
DF$x2 <- ifelse(is.na(DF$x2), 0, DF$x2)
DF
#>   x1 x2
#> 1  1 11
#> 2  2 12
#> 3  3  0
#> 4  4  4
#> 5  5  0

Created on 2020-07-03 by the reprex package (v0.2.1)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.