Error in sum(List) : invalid ‘type’ (list) of argument

Error in sum(List) : invalid ‘type’ (list) of argument, You’ll learn how to fix the “Error in FUN: invalid ‘type’ (list) of argument” in this R lesson.

Example Data Creation

The following list will serve as the foundation for this R tutorial.

Checking Missing Values in R – Data Science Tutorials

List <- list(1:10,    15, 100)
List
[[1]]
 [1]  1  2  3  4  5  6  7  8  9 10
[[2]]
[1] 15
[[3]]
[1] 100

The structure of our example data is seen in the previous RStudio console output — There are three list elements in this list object. There are numeric values in each of these list components.

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

The ” Error in sum(List) : invalid ‘type’ (list) of argument” can be replicated using the R code below.

Assume we wish to calculate the total of all elements in our data. Then, as seen in the following R code, we may use the sum function:

sum(List)                     
Error in sum(List) : invalid 'type' (list) of argument

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

The “Error in FUN: invalid ‘type’ (list) of argument” is demonstrated in this example.

To do so, we must first use the unlist function to transform our list into a vector object.

listvec <- unlist(List)
listvec
1   2   3   4   5   6   7   8   9  10  15 100

The structure of our modified data is shown in the preceding output. We’ve built a vector that contains all of the input list’s elements.

How to draw heatmap in r: Quick and Easy way – Data Science Tutorials

We can now use the sum function for our newly modified data.

sum(listvec)
170

The sum function provides the correct result without any issues this time.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

four + nineteen =