R Error in FUN : invalid ‘type’ (character) of argument (2 Examples)

 

This article explains how to handle the “Error in FUN : invalid ‘type’ (character) of argument” in the R programming language.

The tutorial contains these topics:

Let’s dig in!

 

Example Data

Let’s first create some example data in R:

x <- as.character(1:5)        # Example data
x
# [1] "1" "2" "3" "4" "5"

The previous output of the RStudio console shows that our example data is a vector consisting of five elements.

 

Example 1: Reproduce the Error in FUN : invalid ‘type’ (character) of argument

The following R programming code shows how to replicate the “Error in FUN : invalid ‘type’ (character) of argument”.

Let’s assume that we want to calculate the sum of the numbers in our vector. Then, we might try to apply the sum function as shown below:

sum(x)                        # Apply sum to characters
# Error in sum(x) : invalid 'type' (character) of argument

Unfortunately, the previous R code has returned the error message “Error in FUN : invalid ‘type’ (character) of argument”.

The reason for this is that our data has the wrong data type.

We can check the data type of our vector using the class function:

class(x)                      # Check class of data
# [1] "character"

As you can see, our data has the character class. However, the sum function should be applied to numerical or integer data.

Next, I’ll explain how to solve this problem – so keep on reading!

 

Example 2: Fix the Error in FUN : invalid ‘type’ (character) of argument

The following R syntax shows how to debug the “Error in FUN : invalid ‘type’ (character) of argument”.

For this, we first have to convert the class of our data from character to numeric:

x_num <- as.numeric(x)        # Convert character to numeric

Next, we can apply the sum function to our updated data:

sum(x_num)                    # Apply sum to numericals
# [1] 15

This time the result is returned without any error messages. Great!

 

Video, Further Resources & Summary

Would you like to learn more about the handling of the “Error in FUN : invalid ‘type’ (character) of argument”? Then I recommend watching the following video on my YouTube channel. I show the R programming codes of this tutorial in the video.

 

The YouTube video will be added soon.

 

Also, you might read the other articles on this homepage. You can find a selection of tutorials below:

 

In summary: At this point you should know how to deal with the “Error in FUN : invalid ‘type’ (character) of argument” in R programming. Let me know in the comments below, if you have any further comments or questions.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


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.

Top