What does an “Argument not Numeric or Logical” error mean?

In R programming language, the “Argument not Numeric or Logical Error” occurs when a function is expecting an input that is of numeric data type or of logical data type, but instead receives something else as arguments which may be a string or character, or other non-numeric/ non-logical argument. This error arises when the r has defined functions that attempt mathematical operations and perform logical comparisons based on the input provided and the input is not in the correct format.

From the below mentioned example you can clearly understand under what situation a user can get this type of error.

R
# Create a data frame with student information
student_data <- data.frame(
  name = c("Alice", "Bob", "Charlie"),
  age = c(20, 21, 21),
  test_score = c("80", "90", "75")
)

# Calculate the average test score
average_score <- mean(student_data$test_score)
print(average_score)

Output:

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

[1] NA

A data frame is created containing information about the marks of students.

  • Now through this code we want to calculate the average test score for students.
  • The test_score of students are entered as characters instead of numeric value.
  • We attempt to calculate the average test score using the `mean()` function.
  • However the `mean()` function expected input as numeric data type.
  • Here, the input passed is character hence this results into “Argument not Numeric or Logical” error.

How to fix the above code?

The above code can be improved by simply changing the datatype of test_score from character to numeric.

You can convert the character to numeric in R by using the as.numeric() function.

R
# Create a data frame with student information
student_data <- data.frame(
  name = c("Alice", "Bob", "Charlie"),
  age = c(20, 21, 21),
  test_score = c("80", "90", "75")
)
# Convert the character data type to numeric
student_data$test_score <- as.numeric(student_data$test_score)
# Calculate the average test score
average_score <- mean(student_data$test_score)
message <- paste("The Average Score is: ",average_score)
print(message)

Output:

[1] "The Average Score is:  81.6666666666667"

A data frame is created containing information about the marks of students.

  • Now through this code we want to calculate the average test score for students.
  • The test_score of students are entered as characters instead of numeric value.
  • Now, using the as.numeric() function we will convert the character data type value of the test_score to numeric data type.
  • The average of the test_score can now be calculated using the mean() function.

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 advantages, it has some disadvantages also. One common error that the R programming language has is the “argument not numeric or logical” error. This error occurs when a function expects numeric or logical arguments but receives some other data type such as characters or string.

Similar Reads

What does an “Argument not Numeric or Logical” error mean?

In R programming language, the “Argument not Numeric or Logical Error” occurs when a function is expecting an input that is of numeric data type or of logical data type, but instead receives something else as arguments which may be a string or character, or other non-numeric/ non-logical argument. This error arises when the r has defined functions that attempt mathematical operations and perform logical comparisons based on the input provided and the input is not in the correct format....

Different ways of Dealing with Arguments not Numeric or Logical Error

Method 1: Conversion of Data Type...