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,100 points
78 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,680 points

Related Questions In Data Analytics

+1 vote
1 answer

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

You have missed out on "na.rm=TRUE" inside ...READ MORE

answered Mar 27, 2018 in Data Analytics by Bharani
• 4,550 points
38 views
0 votes
1 answer

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

You have various options to count the ...READ MORE

answered Apr 12, 2018 in Data Analytics by darklord
• 6,140 points
47 views
0 votes
1 answer

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

You can use complete.cases in the following ...READ MORE

answered Apr 13, 2018 in Data Analytics by darklord
• 6,140 points
3,399 views
0 votes
3 answers

How to remove NA values from a Vector in R?

using the function "complete.cases" for example, if you ...READ MORE

answered Jun 18 in Data Analytics by anonymous
4,131 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,700 points
68 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,700 points
38 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,700 points
36 views
0 votes
1 answer

How to sort a data frame by columns in R?

You can just use the order function ...READ MORE

answered Apr 10, 2018 in Data Analytics by darklord
• 6,140 points
61 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,680 points
64 views
0 votes
1 answer

How to sum a variable by group in R?

Easily by using Aggregate Func(): aggregate(x$points, by=list(Players=x$Players), FUN=sum) or ...READ MORE

answered Apr 13, 2018 in Data Analytics by CodingByHeart77
• 3,680 points

edited Apr 13, 2018 by CodingByHeart77 4,321 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.