- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 −
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))
# A tibble: 20 x 2 # Groups: Grp  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
- How to select rows of an R data frame that are non-NA?
- How to replace NA values with zeros in an R data frame?
- How to replace NA with 0 and other values to 1 in an R data frame column?
- How to find the column mean by excluding NA’s and if all values are NA then output NA in R data frame?
- How to extract columns based on particular column values of an R data frame that match\na pattern?
- How to assign a column value in a data frame based on another column in another R data frame?
- How to fill NA values with previous values in an R data frame column?
- How to subset an R data frame with condition based on only one value from categorical column?
- How to create a replacement column with multiple conditions and NA in R data frame?
- Replace numerical column values based on character column values in R data frame.
- How to check which value is NA in an R data frame?
- How to replace NA values in columns of an R data frame form the mean of that column?
- How to replace 0 with NA in an R matrix?
- How to convert NaN to NA in an R data frame?
- How to fill the NA with last observation in the column of an R data frame?