R Error in Function – invalid type character of argument (2 Examples)

In this R programming tutorial you’ll learn how to avoid the “Error in FUN : invalid ‘type’ (character) of argument”.

Example 1: Replicating the Error in Function : invalid ‘type’ (character) of argument

vec_char <- as.character(c(8, 1, 5, 5, 2))    # Example vector in R
vec_char                                      # Display vector
# [1] "8" "1" "5" "5" "2"
sum(vec_char)                                 # sum function leads to error
# Error in sum(vec_char) : invalid 'type' (character) of argument

Example 2: Solving the Error in Function : invalid ‘type’ (character) of argument

vec_num <- as.numeric(vec_char)               # Change data class
sum(vec_num)                                  # Properly apply sum function
# [1] 21

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