Select Page

How to Solve R Error in FUN: invalid ‘type’ (character) of argument

by | Programming, R, Tips

This error occurs if you try to perform a mathematic operation on a character vector. You can solve this error by converting the characters to numeric values using the as.numeric() function. For example,

x <- c("2", "3", "4", "5")
x_num <- as.numeric(x)
sum(x_num)

Alternatively, if the character values represent categories you can use the aggregate() function to group by the categories.

This tutorial will go through the error in detail with code examples.


Example #1

Let’s look at an example to reproduce the error.

# Define a character vector
x <- c("2", "3", "4", "5")

# Attempt to calculate sum
sum(x)

Let’s run the code to see what happens:

Error in sum(x) : invalid 'type' (character) of argument

The error occurs because mathematical operations like sum() are only suitable for numeric vectors. The vector x is of a character vector, which we can confirm using the class() function.

class(x)
[1] "character"

Solution

We can solve the error by converting the character vector to a numeric vector using the as.numeric() function. Let’s look at the revised code:

x <- c("2", "3", "4", "5")
x_num <- as.numeric(x)
sum(x_num)

Let’s run the code to get the sum:

[1] 14

We can confirm that x_num is a numeric vector using the class() function:

class(x_num)
[1] "numeric"

Example #2

Let’s look at a second example to reproduce the error:

# Define data frame 
df <- data.frame(fruit=c('pineapple', 'apple', 'pineapple', 'blueberry', 'apple', 'apple', 'pear', 'blueberry'),
                 amount_sold=c(75, 50, 89, 76, 45, 98, 58, 64))

# Calculate some of values in fruit column
sum(df$fruit)

Let’s run the code to see what happens:

Error in sum(df$fruit) : invalid 'type' (character) of argument

The error occurs because the column 'fruit' is a character column, which we can confirm using the class() function:

class(df$fruit)
[1] "character"

Solution

We can solve the error by only using mathematical operations with numeric vectors. We could use the sum() function on the amount_sold column as it is numeric.

df <- data.frame(fruit=c('pineapple', 'apple', 'pineapple', 'blueberry', 'apple', 'apple', 'pear', 'blueberry'),
                 amount_sold=c(75, 50, 89, 76, 45, 98, 58, 64))

sum(df$amount_sold)
               
[1] 555

We can also use the aggregate function to group the rows in the data frame by fruit and calculate the sum of the amount_sold for each category.

aggregate(amount_sold ~ fruit, df, sum)

Let’s run the code to see the result:

  fruit amount_sold
1     apple         193
2 blueberry         140
3      pear          58
4 pineapple         164

We successfully summed up the amount sold for each type of fruit in the data frame.

Summary

Congratulations on reading to the end of this tutorial!

For further reading on R-related errors, go to the articles: 

Go to the online courses page on R to learn more about coding in R for data science and machine learning.

Have fun and happy researching!