- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 NA’s to a value of selected columns in an R data frame?
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
- Related Articles
- How to standardize selected columns in R data frame?
- How to replace zero with previous value in an R data frame column?
- How to find the row mean for selected columns in R data frame?
- How to find the number of NA’s in each column of an R data frame?
- How to replace a particular value in R data frame with a new value?
- How to find the percent of NA’s in R data frame rows?
- How to replace a complete column in an R data frame?
- How to standardize columns in an R data frame?
- How to deal with error “undefined columns selected when subsetting data frame” in R?
- How to add a prefix to columns of an R data frame?
- How to subset an R data frame by ignoring a value in one of the columns?
- How to replace NA values in columns of an R data frame form the mean of that column?
- How to reorder the columns in an R data frame?
- How to subset factor columns in an R data frame?
- How to concatenate numerical columns in an R data frame?

Advertisements