I have a data frame with two columns. When I try to calculate mean
, I get this message:
[1] NA Warning message: In mean.default(results) : argument is not numeric or logical: returning NA`
where 'results' is my data set. Any advice on getting around this problem?
The “Argument is not numeric or logical: returning na” appears when you use non-numerical or illogical objects rather than numerical or logical objects to calculate the mean in R.
In this method of computing, the mean of the given dataframe column user just need to call the colMeans function which is an in-build function in R language and pass the dataframe as its parameter, then this will be returning the mean of all the column in the provided dataframe to the user.
In R, the is. numeric() function returns a logical value, TRUE or FALSE , indicating if the argument passed to it has a base type of the class double or integer and the values are regarded as numeric.
To convert factors to the numeric value in R, use the as. numeric() function. If the input is a vector, then use the factor() method to convert it into the factor and then use the as. numeric() method to convert the factor into numeric values.
From R 3.0.0 onwards mean(<data.frame>)
is defunct (and passing a data.frame to mean
will give the error you state)
A data frame is a list of variables of the same number of rows with unique row names, given class "data.frame".
In your case, result has two variables (if your description is correct) . You could obtain the column means by using any of the following
lapply(results, mean, na.rm = TRUE) sapply(results, mean, na.rm = TRUE) colMeans(results, na.rm = TRUE)
If you just want to know the mean, you can use
summary(results)
It will give you more information than expected.
ex) Mininum value, 1st Qu., Median, Mean, 3rd Qu. Maxinum value, number of NAs.
Furthermore, If you want to get mean values of each column, you can simply use the method below.
mean(results$columnName, na.rm = TRUE)
That will return mean value. (you have to change 'columnName' to your variable name
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With