How to Fix in R: Argument is not numeric or logical: returning na
Last Updated :
18 Mar, 2022
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.
When this error might occur
Let's create a data frame first:
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))
# Print the dataframe
print(dataframe)
Output:
Output
Now we will try to compute the mean of the values in the section column.
Example:
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 in
# section column
mean(dataframe$team)
Output:
Output
As you can see in the output, the R compiler produces "argument is not numeric or logical: returning NA" as the section has non-numeric values.
Let's compute the mean of the data frame now:
Example:
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
# of the dataframe
mean(dataframe)
Output:
OutputHow to avoid this warning ?
The only way to avoid this warning is to use the mean() function with vectors having numeric values only. For example, In the above example we can calculate the mean of the minor column as it contains numerical values only:
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
mean(dataframe$minor)
Output:
OutputMean 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:
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:
Output
This time the program compiled successfully without any warning message.
Similar Reads
How to solve Argument not Numeric or Logical Error in R R is a powerful programming language commonly used for statistical computing, data analysis, and graphical representation. It is highly extensible through packages contributed by a vast community of users. Key features of the R programming language include However R Programming Language has many adv
7 min read
How to Fix in R: Arguments imply differing number of rows In this article, we are going to see how to fix  Arguments that imply differing numbers of rows in R Programming Language. One error that we may encounter in R is: arguments imply differing number of rows: 6, 5 The compiler produces such an error when we try to create a data frame and the number of
2 min read
How to Fix: âxâ must be numeric in R In this article, we are going to see how to fix: 'x' must be numeric. For this we will cover two example for the error message âx must be numericâ. Example 1: Error in vector 'x' must be numeric In this example, we will create a vector and try to plot a hist() plot with particular data and then occu
2 min read
How to Prevent ifelse() from Turning Date Objects into Numeric Objects in R? The ifelse() function in R is widely used for vectorized conditional operations. However, a common issue arises when you apply ifelse() to Date objects: it converts them into numeric objects, representing the number of days since January 1, 1970. This behavior can be problematic when you want to mai
2 min read
How to Fix: Error in select unused arguments in R? In this article, we will be looking toward the approach to fix the error in selecting unused arguments in the R programming language. Error in selecting unused arguments: The R compiler produces this error when a programmer tries to use the select() function of the dplyr package in R provided that t
2 min read
How to Manage Argument Length Zero Error in R Programming In R Programming Language, encountering errors is common, especially for beginners and seasoned developers. One such error that often perplexes programmers is the Argument Length Zero Error. This error occurs when a function or operation expects arguments to be provided, but none are supplied, resul
3 min read