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

[This article was first published on Data Science Tutorials, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post Error in sum(List) : invalid ‘type’ (list) of argument appeared first on
Data Science Tutorials –

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.

The post Error in sum(List) : invalid ‘type’ (list) of argument appeared first on Data Science Tutorials

To leave a comment for the author, please follow the link and comment on their blog: Data Science Tutorials.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)