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" |
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 |
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 |
vec_num <- as.numeric(vec_char) # Change data class
sum(vec_num) # Properly apply sum function # [1] 21 |
sum(vec_num) # Properly apply sum function # [1] 21