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,350 points
1,975 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,660 points
0 votes

Try this.

lapply(a,function(x){ifelse(is.na(x),mean(a,na.rm = TRUE),x) })
answered Aug 14, 2019 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,750 points
4,592 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
5,050 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, 2019 in Data Analytics by anonymous
• 33,030 points
788 views
0 votes
1 answer

Counting the number of elements with the values of x in a vector

You may simply use the table() method: > ...READ MORE

answered Jun 14, 2022 in Data Analytics by Sohail
• 3,040 points
634 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,660 points
1,224 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,660 points
5,909 views
0 votes
1 answer

Left Join and Right Join using "dplyr"

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

answered Mar 27, 2018 in Data Analytics by Bharani
• 4,660 points
1,156 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,660 points
1,440 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,660 points
1,706 views
+1 vote
2 answers

Finding number of missing values and removing those missing values from a data-frame

To find number of missing values for ...READ MORE

answered Aug 14, 2019 in Data Analytics by anonymous
1,115 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