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

+1 vote

I have this vector:

a<-c(1,2,3,NA,4,5,NA,NA,100,45,NA)

and I'm trying to replace all the missing values present in this vector by writing this custom function:

mean_impute<-function(x){
ifelse(is.na(x),mean(x),x)
}

But the result obtained has NA values in it. What is wrong with this function?

Mar 27, 2018 in Data Analytics by kurt_cobain
• 9,260 points
97 views

2 answers to this question.

0 votes

You have missed out on "na.rm=TRUE" inside the mean function.

So, your should look like this:

mean_impute<-function(x){
ifelse(is.na(x),mean(x,na.rm = T),x)
}

And this code will give you the desired output which is:

1.00000   2.00000   3.00000  22.85714   4.00000   5.00000  22.85714  22.85714 100.00000  45.00000  22.85714
answered Mar 27, 2018 by Bharani
• 4,550 points
0 votes

Try this.

lapply(a,function(x){ifelse(is.na(x),mean(a,na.rm = TRUE),x) })
answered Aug 14 by anonymous

Related Questions In Data Analytics

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,690 points
186 views
0 votes
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 in Data Analytics by anonymous
• 31,840 points
129 views
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 in Data Analytics by anonymous
• 31,840 points
21 views
0 votes
1 answer

How to replace NA values in a dataframe with Zero's ?

It is simple and easy: df1<-as.data.frame(matrix(sample(c(NA, 1:10), 100, ...READ MORE

answered Apr 10, 2018 in Data Analytics by CodingByHeart77
• 3,690 points
141 views
0 votes
2 answers

Transforming a key/value string into distinct rows in R

We would start off by loading the ...READ MORE

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

Finding frequency of observations in R

You can use the "dplyr" package to ...READ MORE

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

Left Join and Right Join using "dplyr"

The below is the code to perform ...READ MORE

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

Plotting multiple graphs on the same page in R

If you want to plot 4 graphs ...READ MORE

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

Applying the same function to every row of a data.frame - R

You can use the 'appply()' function for ...READ MORE

answered May 22, 2018 in Data Analytics by Bharani
• 4,550 points
72 views
+1 vote
2 answers