Mean using sapply() function
R provides the sapply() function to compute the mean of values in every column of the data frame.
R
# Create a data frame dataframe <- data.frame (students= c ( 'Bhuwanesh' , 'Anil' , 'Suraj' , 'Piyush' , 'Dheeraj' ), section= c ( 'A' , 'A' , 'C' , 'C' , 'B' ), minor= c (87, 98, 71, 89, 82), major= c (80, 88, 84, 74, 70)) # Try to calculate mean of values stored # at the column minor in the dataframe sapply (df, mean, 2) |
Output:
As you can see in the output, the R compiler produces the warning message but also calculate the mean values of the columns having numeric values in it. We can avoid the warning completely by explicitly specifying the columns having numeric values.
R
# Create a data frame dataframe <- data.frame (students= c ( 'Bhuwanesh' , 'Anil' , 'Suraj' , 'Piyush' , 'Dheeraj' ), section= c ( 'A' , 'A' , 'C' , 'C' , 'B' ), minor= c (87, 98, 71, 89, 82), major= c (80, 88, 84, 74, 70)) # Try to calculate mean of values stored # at the column minor in the dataframe sapply (dataframe[ c ( 'minor' , 'major' )], mean, 2) |
Output:
This time the program compiled successfully without any warning message.
How to Fix in R: Argument is not numeric or logical: returning na
In this article, we are going to see how to fix in R argument is not numeric or logical returning na.
This is the warning message that you may face in R. It takes the following form:
Warning message:
In mean.default(dataframe) : argument is not numeric or logical: returning NA
The R compiler produces such type of error when we try to compute the mean of an object in R but the object contains non-numerical or illogical values. This article focuses on how we can fix this error.
Contact Us