Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum Elements of a List in R - For

Tags:

r

I have a list (mylist) of 110 items.

length(mylist)

#[1] 110

The elements within the lists are descriptions. I'm trying sum the elements of the entire list (basically counting the words in each description).

This provides a total for the first element.

length(mylist[[1]]) 

#[1] 162

Instead of repeating that 110 times, what is the best way to sum all 110 elements? Would a for statement work? Thanks.

like image 385
J Church Avatar asked Sep 11 '25 00:09

J Church


2 Answers

More generally, use lapply to apply a function to each element of a list:

lapply(list, length)
like image 109
divibisan Avatar answered Sep 12 '25 14:09

divibisan


If you want to determine element (word) wise count of each component (description) in your list , use Reduce("+",mylist)

like image 44
Akankhya Mohapatra Avatar answered Sep 12 '25 15:09

Akankhya Mohapatra