How to replace NAs with non-NA if there is only one non-NA in R data frame based on another column?


If we have only one non-NA value in an R data frame column and a column that is categorical then we might want to replace the NAs with the given non-NA. For this purpose, we can follow the below steps −

  • First of all, creating a data frame
  • Then replacing the NA with non-NA with the help of min function of base R and mutate function of dplyr package.

Create the data frame

Let's create a data frame as shown below −

 Live Demo

Grp<-rep(LETTERS[1:4],each=5)
Response<-rep(c(1,NA,3,NA,5,NA,10,NA),times=c(1,4,1,4,1,4,1,4))
df<-data.frame(Grp,Response)
df

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

 Grp Response
1 A    1
2 A    NA
3 A    NA
4 A    NA
5 A    NA
6 B    3
7 B    NA
8 B    NA
9 B    NA
10 B   NA
11 C    5
12 C   NA
13 C   NA
14 C   NA
15 C   NA
16 D   10
17 D   NA
18 D   NA
19 D   NA
20 D   NA

Replacing NA with Non-NA based on another column

Replacing the NA with non-NA in Response column based on Grp column −

Grp<-rep(LETTERS[1:4],each=5)
Response<-rep(c(1,NA,3,NA,5,NA,10,NA),times=c(1,4,1,4,1,4,1,4))
df<-data.frame(Grp,Response)
library(dplyr)
df%>%group_by(Grp)%>%mutate(Response=min(Response,na.rm=TRUE))

Output

# A tibble: 20 x 2
# Groups: Grp [4]
Grp Response
<chr> <dbl>
1  A    1
2  A    1
3  A    1
4  A    1
5  A    1
6  B    3
7  B    3
8  B    3
9  B    3
10 B    3
11 C    5
12 C    5
13 C    5
14 C    5
15 C    5
16 D    10
17 D    10
18 D    10
19 D    10
20 D    10

Updated on: 13-Aug-2021

255 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements