- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 values with zeros in an R data frame?
We can replace all NA values by using is.na function
Example
> Data <- matrix(sample(c(NA, 0:9), 100, replace = TRUE), 10) > df<-as.data.frame(Data) > df V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 1 9 7 3 0 3 7 7 3 9 9 2 9 2 3 0 2 0 1 4 6 7 3 5 0 9 2 4 8 8 7 NA 5 4 7 3 1 2 6 NA 7 1 1 8 5 3 2 9 6 4 7 0 5 6 1 6 8 5 6 5 3 9 6 0 7 0 7 8 3 4 NA NA 0 2 4 2 NA 8 6 9 9 9 4 0 6 1 7 NA 9 5 5 NA 8 1 NA 0 9 9 3 10 1 1 0 7 1 1 4 1 2 1
Replacing NA’s by 0’s
> df[is.na(df)] <- 0 > df V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 1 9 7 3 0 3 7 7 3 9 9 2 9 2 3 0 2 0 1 4 6 7 3 5 0 9 2 4 8 8 7 0 5 4 7 3 1 2 6 0 7 1 1 8 5 3 2 9 6 4 7 0 5 6 1 6 8 5 6 5 3 9 6 0 7 0 7 8 3 4 0 0 0 2 4 2 0 8 6 9 9 9 4 0 6 1 7 0 9 5 5 0 8 1 0 0 9 9 3 10 1 1 0 7 1 1 4 1 2 1
- Related Articles
- How to replace NA with 0 and other values to 1 in an R data frame column?
- How to fill NA values with previous values in an R data frame column?
- How to convert empty values to NA in an R data frame?
- How to convert NaN values to NA in an R data frame?
- How to replace missing values with median in an R data frame column?
- How to replace missing values with row means 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 randomly replace values in an R data frame column?
- How to replace $ sign combined with some specific values in an R data frame?
- How to fill the NA values from above row values in an R data frame?
- How to calculate row means by excluding NA values in an R data frame?
- How to find the frequency of NA values per row in an R data frame?
- How to replace missing values in a column with corresponding values in other column of an R data frame?
- How to convert NaN to NA in an R data frame?
- How to replace 0 with NA in an R matrix?

Advertisements