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

 

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

The article is structured as follows:

Let’s dive into it:

 

Creation of Example Data

We’ll use the following list as basement for this R tutorial:

my_list <- list(1:5,              # Create example list
                3,
                10)
my_list                           # Print example list
# [[1]]
# [1] 1 2 3 4 5
# 
# [[2]]
# [1] 3
# 
# [[3]]
# [1] 10

The previous output of the RStudio console shows the structure of our example data – It’s a list object containing three list elements. Each of these list elements contains numeric values.

 

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

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

Let’s assume that we want to get the sum of all elements in our data. Then, we might try to apply the sum function as shown in the following R code:

sum(my_list)                      # Try to apply sum function
# Error in sum(my_list) : invalid 'type' (list) of argument

As you can see, the RStudio console returns the error message “Error in sum(my_list) : invalid ‘type’ (list) of argument” after executing the previous syntax.

The reason for this is that the sum function should be applied to vectors instead of lists.

In the next example, I’ll show how to solve this problem.

 

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

This example shows how to handle the “Error in FUN : invalid ‘type’ (list) of argument”.

For this, we first have to convert our list to a vector object using the unlist function:

my_list_vec <- unlist(my_list)    # Unlist list elements
my_list_vec                       # Print updated data
# [1]  1  2  3  4  5  3 10

The previous output shows the structure of our updated data. We have created a vector containing all elements of the input list.

In the next step, we can apply the sum function to our updated data:

sum(my_list_vec)                  # Properly apply sum function
# [1] 28

This time, the sum function returns the proper result without any problems.

 

Video & Further Resources

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

 

The YouTube video will be added soon.

 

In addition, you might read the other tutorials on my website. Please find a selection of articles below.

 

Summary: You have learned in this article how to debug the “Error in FUN : invalid ‘type’ (list) of argument” in R programming. Let me know in the comments below, if you have any additional questions. Furthermore, please subscribe to my email newsletter in order to receive updates on new tutorials.

 

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.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top