How to replace NA values in a dataframe with Zero s

0 votes

I have a dataframe which consist of NA values, want to replace that values with zero's?

> as.data.frame(matrix(sample(c(NA, 1:10), 100, replace = TRUE), 10))
   V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
1   9 NA  9 NA NA  6  3 NA  6   8
2   8  8  4  6  9  5  9  4 10   7
3  10  7  7 10  3 NA 10 NA  5  10
4   6  6 NA  9 10  1  4  5 NA   2
5   9  3  3  6  7 10  6  3  2   1
6   4  3  2  5  3 NA  7  7  2   4
7   4  1  8  9  5  1  5  8 NA  NA
8   7  5  4 NA  8 10  3  5  9   2
9   4  3  1  5  5  2  8  3  6   2
10  3 10  3  3  5  6  2 NA  3   4
Apr 10, 2018 in Data Analytics by DataKing99
• 8,240 points
1,766 views

1 answer to this question.

0 votes

It is simple and easy:

df1<-as.data.frame(matrix(sample(c(NA, 1:10), 100, replace = TRUE), 10))
df1[is.na(df1)] <- 0
> df1
   V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
1   3  7  1  1  9  2  3  8  7   2
2  10  1  0  5  6  3  9  9  5   1
3   2 10  0  4  4  3  8  9  3   8
4   5  7  2  3  9  4  9  8  2   5
5   5  2  2  3  9 10  6  4  3   7
6   2  4  1  1  5  3  6  4  7  10
7   2  0  2  7  2  4  6  6  7   8
8   2  5  0  2 10  4  2  6  1   3
9   0  3  3  6  7  5  3  3  1   1
10  0  7  1  0  2  2  4  9  4   7

Note*: Here random sample has created new values.


answered Apr 10, 2018 by CodingByHeart77
• 3,740 points

Related Questions In Data Analytics

0 votes
1 answer

How to replace only set of values in a vector to different values in a dataframe?

If you have a condition using which ...READ MORE

answered Aug 19, 2019 in Data Analytics by anonymous
• 33,030 points
555 views
+1 vote
2 answers

Custom Function to replace missing values in a vector with the mean of values

Try this. lapply(a,function(x){ifelse(is.na(x),mean(a,na.rm = TRUE ...READ MORE

answered Aug 14, 2019 in Data Analytics by anonymous
1,610 views
+1 vote
2 answers

How to count the number of elements with the values in a vector?

Use dplyr function group_by(). > n = as.data.frame(num) > ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
4,527 views
0 votes
2 answers

How to remove rows with missing values (NAs) in a data frame?

Hi, The below code returns rows without ...READ MORE

answered Aug 20, 2019 in Data Analytics by anonymous
• 33,030 points
14,385 views
0 votes
1 answer

How to convert a text mining termDocumentMatrix into excel or csv in R?

By assuming that all the values are ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,589 views
0 votes
1 answer

How to create a list of Data frames?

Basically all we have to do is ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,720 points
983 views
0 votes
1 answer

How to spilt a column of a data frame into multiple columns

it is easily achievable by using "stringr" ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,432 views
+1 vote
2 answers

How to sort a data frame by columns in R?

You can use dplyr function arrange() like ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
1,401 views
0 votes
1 answer

How to write a custom function which will replace all the missing values in a vector with the mean of values in R?

Consider this vector: a<-c(1,2,3,NA,4,5,NA,NA) Write the function to impute ...READ MORE

answered Jul 4, 2018 in Data Analytics by CodingByHeart77
• 3,740 points
4,190 views
+4 votes
3 answers

How to sum a variable by group in R?

You can also try this way, x_new = ...READ MORE

answered Aug 1, 2019 in Data Analytics by Cherukuri
• 33,030 points
77,167 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP