# Dealing with NA value

#1

I have dataset in which multiple(8) variables with same class have NA values. So how do I deal with the all the variables's NA values in just one loop. I have tried with this code, but It didn't work.

sapply(final_data,function(x){
x[is.na(x)]<-mean(x,na.rm=TRUE)
})
It has just returned mean values for each variable present in data.
Please let me know is there any alternative way to handle such all NA values with one time.

0 Likes

#2

Here is one way to do it using sapply()

``````df <- data.frame(A = c(1,2,NA,4), B = c(5,NA,3,1), C = c(NA,2,3,NA))
df
#>    A  B  C
#> 1  1  5 NA
#> 2  2 NA  2
#> 3 NA  3  3
#> 4  4  1 NA
df[] <- sapply(df, function(x) ifelse(is.na(x), mean(x, na.rm = TRUE), x))
df
#>          A B   C
#> 1 1.000000 5 2.5
#> 2 2.000000 3 2.0
#> 3 2.333333 3 3.0
#> 4 4.000000 1 2.5
``````

Created on 2019-04-20 by the reprex package (v0.2.1)
However, I would be cautious about calculating further results after replacing NA with the column mean.

1 Like

#3

Another way to do it using `sapply`, but using `replace` instead of `ifelse`:

``````df <- data.frame(A = c(1, 2, NA, 4),
B = c(5, NA, 3, 1),
C = c(NA, 2, 3, NA))

(df <- sapply(X = df,
FUN = function(t)
replace(x = t,
list = is.na(x = t),
values = mean(x = t,
na.rm = TRUE))))
#>             A B   C
#> [1,] 1.000000 5 2.5
#> [2,] 2.000000 3 2.0
#> [3,] 2.333333 3 3.0
#> [4,] 4.000000 1 2.5
``````
1 Like

#4

I'd recommend doing this:

``````library('tidyverse')
d = tibble(x1 = c(1, 2, 3, NA, 5, 6))
d %>% mutate(x1 = x1 %>% replace_na(mean(x1, na.rm = TRUE)))
``````

It's more intuitive and self-explanatory

2 Likes

#5

Thank You for your response! It worked, However, that changed my class of dataset into the matrix and then I have changed to data.frame class and did the other calculations.

0 Likes

#6
``````df[] <- sapply(df, function(x) ifelse(is.na(x), mean(x, na.rm = TRUE), x))
``````

The empty square brackets after df on the left side should prevent the output of sapply being a matrix. Did you include those in your code?

1 Like

#7

I did forget to include that, and due to that it converted to matrix class, thanks for letting me know!

0 Likes

closed #8

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

0 Likes