-1

I have a data.frame "nitrates". And I have to calculate the mean of the values. When I use:

mean(nitrates)

it gives me NA with the warning:

Warning message: In mean.default(nitrates) : argument is not numeric or logical: returning NA

I want to calculate the mean of data. How can I do that?

1 Answers1

0

Let say you have a dataframe containing mixed string and numeric columns. Since mean is defined for numeric values, you need to first select numeric columns and then move forward with averaging. I don't have your dataframe, so I provide an example with another dataframe, but you can replace storms with nitrates.

library('dplyr')
data('storms')

# mean for each column
storms %>% select_if(is.numeric) %>% apply(2, mean, na.rm=T)

# mean for each row
storms %>% select_if(is.numeric) %>% apply(1, mean, na.rm=T)

# mean over all elements
storms %>% select_if(is.numeric) %>% as.matrix() %>% mean(na.rm=T)

Reza
  • 1,945
  • 1
  • 9
  • 17