Remove NA values from the output in R programming

+1 vote

I'm trying to find mean of all column values using R and I got the following output:

[1]        NA        NA  9.957516 77.882353  6.993464 15.803922

I dont want the NA values to be shows in the output, how do I go about it?

This is my code:

columnmean <- function(x) {
                nc <- ncol(x)
                means <- numeric(nc)
                for(i in 1:nc) {
                                means[i] <- mean(x[, i])
                }
                means
}
Oct 31, 2018 in Data Analytics by Hannah
• 18,520 points
879 views

1 answer to this question.

0 votes

Edit your code:

columnmean <- function(x, removeNA = TRUE) {
                nc <- ncol(x)
                means <- numeric(nc)
                for(i in 1:nc) {
                                means[i] <- mean(x[, i], na.rm = removeNA)
                }
                means
}
answered Oct 31, 2018 by Kalgi
• 52,350 points

Related Questions In Data Analytics

0 votes
1 answer
+1 vote
5 answers

How to remove NA values from a Vector in R?

Hello team, you can use na.omit x <- c(NA, 3, ...READ MORE

answered Dec 9, 2020 in Data Analytics by anonymous
• 82,840 points
195,126 views
0 votes
1 answer

How to remove HTML tags from string in R Programming?

Try this simple code to remove the <br ...READ MORE

answered Feb 1, 2019 in Data Analytics by Sophie may
• 10,620 points
4,679 views
0 votes
1 answer

How can I delete multiple values from a vector in R?

The %in% operator tells  which elements are ...READ MORE

answered Apr 27, 2018 in Data Analytics by shams
• 3,670 points
6,181 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,813 views
+10 votes
3 answers

Which is a better initiative to learn data science: Python or R?

Well it truly depends on your requirement, If ...READ MORE

answered Aug 9, 2018 in Data Analytics by Abhi
• 3,720 points
1,626 views
0 votes
1 answer

How can I perform word stemming in R

The tm package in R provides the stemDocument() function to stem the ...READ MORE

answered Aug 20, 2018 in Data Analytics by Abhi
• 3,720 points
4,537 views
0 votes
1 answer

How to know the type of an object?

To find the type of an object ...READ MORE

answered Sep 20, 2018 in Data Analytics by Abhi
• 3,720 points
712 views
0 votes
1 answer

"no applicable method" Error in r programming

This is caused by using an object-oriented ...READ MORE

answered Oct 31, 2018 in Data Analytics by Kalgi
• 52,350 points
2,530 views
0 votes
1 answer

": cannot open the connection" Error in R

There can be two reasons for this ...READ MORE

answered Oct 31, 2018 in Data Analytics by Kalgi
• 52,350 points
5,070 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