tg110
tg110

Reputation: 411

adding elements in vectors (not matrices) in a list in R

Consider I have the following list in which each element of the list is a vector and not a matrix (with s number of cols and rows) -

$a
[1] 1 2 3 4 5 

$b
[1] 1 2 3 4 5

$c
[1] 1 2 3 4 5

I want to add across the elements (1,2 3..) of each vector (a, b, c) such that-

$Total
[1] 3 6 9 12 15

How do I do this in R?

Upvotes: 0

Views: 84

Answers (1)

Zheyuan Li
Zheyuan Li

Reputation: 73405

Here is one way:

rowSums(simplify2array(lst))

where lst is your list.

Upvotes: 1

Related Questions