R Warning: argument is not numeric or logical (2 Examples)

In this article you’ll learn how to deal with the warning “argument is not numeric or logical: returning NA” in the R programming language.

Example Data

data(iris)                # Loading some example data
iris_num <- iris[ , 1:4]
head(iris_num)
#   Sepal.Length Sepal.Width Petal.Length Petal.Width
# 1          5.1         3.5          1.4         0.2
# 2          4.9         3.0          1.4         0.2
# 3          4.7         3.2          1.3         0.2
# 4          4.6         3.1          1.5         0.2
# 5          5.0         3.6          1.4         0.2
# 6          5.4         3.9          1.7         0.4

Example 1: Replicating the Warning – argument is not numeric or logical: returning NA

mean(iris_num)            # Trying to apply mean function to data frame
# [1] NA
# Warning message:
#   In mean.default(iris_num) :
#   argument is not numeric or logical: returning NA

Example 2: Solving the Warning – argument is not numeric or logical: returning NA

colMeans(iris_num)        # Applying colMeans function to data frame
# Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
#     5.843333     3.057333     3.758000     1.199333

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top