Remove NA values from the output in R programming

0 votes

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
• 15,620 points
47 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
• 41,760 points

Related Questions In Data Analytics

0 votes
4 answers

How to remove NA values from a Vector in R?

You can try na.omit() or na.exclude() too. ...READ MORE

answered Jul 31 in Data Analytics by anonymous
• 28,280 points
7,328 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 in Data Analytics by Sophie may
• 9,850 points
349 views
0 votes
0 answers
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 26, 2018 in Data Analytics by shams
• 3,580 points
227 views
0 votes
2 answers

How to sort a data frame by columns in R?

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

answered Aug 21 in Data Analytics by anonymous
• 28,280 points
134 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 Anmol
• 3,620 points
66 views
0 votes
1 answer

How to know the type of an object?

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

answered Sep 19, 2018 in Data Analytics by Anmol
• 3,620 points
36 views
0 votes
1 answer

what causes these cannot open errors in R?

They are caused by attempts to read a ...READ MORE

answered Oct 31, 2018 in Data Analytics by Kalgi
• 41,760 points
75 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
• 41,760 points
87 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
• 41,760 points
524 views