Add list elments recursively

0 votes
How to find the sum of nested list elements?

I tried sum function but doesn't work for finding the sum of a nested list.
Oct 23, 2019 in Data Analytics by anonymous
• 3,450 points
109 views

1 answer to this question.

0 votes

You cannot use the sum function on nested lists. If you want to find the total sum of nested list elements the unlist the nested lists and then use sum function.

> list(1,1:5,6)
[[1]]
[1] 1

[[2]]
[1] 1 2 3 4 5

[[3]]
[1] 6

> sum(list(1,1:5,6))
Error in sum(list(1, 1:5, 6)) : invalid 'type' (list) of argument
> sum(unlist(list(1,1:5,6)))
[1] 22
answered Oct 28, 2019 by Cherukuri
• 32,930 points

Related Questions In Data Analytics

0 votes
1 answer

How to provide names for list elments in r?

There are 2 ways to add names ...READ MORE

answered Oct 14, 2019 in Data Analytics by Cherukuri
• 32,930 points
98 views
+1 vote
1 answer

How to convert a list of vectors with various length into a Data.Frame?

We can easily use this command as.data.frame(lapply(d1, "length< ...READ MORE

answered Apr 4, 2018 in Data Analytics by DeepCoder786
• 1,720 points
581 views
0 votes
1 answer

How can I use parallel so that it preserves the list of data frames

You can use pmap as follows: nc <- ...READ MORE

answered Apr 4, 2018 in Data Analytics by kappa3010
• 2,090 points
168 views
0 votes
1 answer

How to join two tables (tibbles) by *list* columns in R

You can use the hash from digest ...READ MORE

answered Apr 5, 2018 in Data Analytics by kappa3010
• 2,090 points
539 views
0 votes
1 answer

By using dpylr package sum of multiple columns

Basically here we are making an equation ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,257 views
0 votes
1 answer

How to convert a text mining termDocumentMatrix into excel or csv in R?

By assuming that all the values are ...READ MORE

answered Apr 5, 2018 in Data Analytics by DeepCoder786
• 1,720 points
660 views
0 votes
1 answer

In a dpylr pipline how to use sample and seq?

For avoiding rowwise(), I prefer to use ...READ MORE

answered Apr 6, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Jun 9, 2020 by Gitika 261 views
0 votes
1 answer

How to create a list of Data frames?

Basically all we have to do is ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,720 points
426 views
0 votes
1 answer

How to add elements into a list in R?

hey, Add elements into list as below. list = ...READ MORE

answered Sep 30, 2019 in Data Analytics by anonymous
• 32,930 points
195 views
0 votes
1 answer

How to add special characters to a list?

@ch, You can add $. , / using ...READ MORE

answered Oct 14, 2019 in Data Analytics by anonymous
• 32,930 points
1,019 views