How to replace NA’s to a value of selected columns in an R data frame?

R ProgrammingServer Side ProgrammingProgramming

In data analysis, finding some NA values in a data frame is very common but all the NA values do not create problems if the column that contain NA values is not useful for the analysis. We can replace all NA values to 0 or to any other for the columns that are useful.

Example

Consider the below data frame −

> set.seed(99)
> x1<-sample(c(5,10,15,NA),20,replace=TRUE)
> x2<-sample(c(1,2,3,NA),20,replace=TRUE)
> x3<-sample(c(20,21,22,23,24,25,NA),20,replace=TRUE)
> x4<-sample(c(letters[1:10],NA),20,replace=TRUE)
> x5<-sample(c(1:10,NA),20,replace=TRUE)
> df<-data.frame(x1,x2,x3,x4,x5)
> df
  x1   x2   x3   x4    x5
1  NA   NA   25  <NA>  NA
2  5     2   24    f    2
3  NA    2   25    i    7
4  10   NA   23    i   10
5  10    1   21    c    3
6   5   NA   NA    h   NA
7  15    2   20    g   10
8  10   NA   25    d   10
9  10    2   23    c    5
10 10    1   NA    f    8
11 NA    3   25  <NA>   5
12 10    2   NA    h    4
13 NA    3   25    g    1
14 5     2   NA    c    8
15 NA   2   NA  <NA>    3
16 NA   NA  23    f     7
17 15   1   24  <NA>    9
18 NA   NA   NA    b    3
19 5    3    NA    d    3    
20 10   2    20    g    8

Changing NA’s to zero of consecutive columns −

> df[,c("x1","x2")][is.na(df[,c("x1","x2")])] <- 0
> df
x1 x2 x3 x4 x5
1 0 0 25 <NA> NA
2 5 2 24 f 2
3 0 2 25 i 7
4 10 0 23 i 10
5 10 1 21 c 3
6 5 0 NA h NA
7 15 2 20 g 10
8 10 0 25 d 10
9 10 2 23 c 5
10 10 1 NA f 8
11 0 3 25 <NA> 5
12 10 2 NA h 4
13 0 3 25 g 1
14 5 2 NA c 8
15 0 2 NA <NA> 3
16 0 0 23 f 7
17 15 1 24 <NA> 9
18 0 0 NA b 3
19 5 3 NA d 3
20 10 2 20 g 8

Changing NA’s to zero of non-consecutive columns −

> df[,c("x3","x5")][is.na(df[,c("x3","x5")])] <- 0
> df
x1 x2 x3 x4 x5
1 0 0 25 <NA> 0
2 5 2 24 f 2
3 0 2 25 i 7
4 10 0 23 i 10
5 10 1 21 c 3
6 5 0 0 h 0
7 15 2 20 g 10
8 10 0 25 d 10
9 10 2 23 c 5
10 10 1 0 f 8
11 0 3 25 <NA> 5
12 10 2 0 h 4
13 0 3 25 g 1
14 5 2 0 c 8
15 0 2 0 <NA> 3
16 0 0 23 f 7
17 15 1 24 <NA> 9
18 0 0 0 b 3
19 5 3 0 d 3
20 10 2 20 g 8
raja
Published on 11-Aug-2020 06:43:44
Advertisements