# [R] Apply a function to a list

arun smartpink111 at yahoo.com
Wed Nov 13 00:06:05 CET 2013

```Hi,
You may also flatten the list of lists and apply the function. In the example I provided,
lapply(do.call(c,unlist(lst1,recursive=FALSE)),sum)

#or
use one of the functions from this link
http://stackoverflow.com/questions/8139677/how-to-flatten-a-list-to-a-list-without-coercion

lapply(flatten2(lst1),sum)
A.K.

On Tuesday, November 12, 2013 5:05 PM, arun <smartpink111 at yahoo.com> wrote:
Hi,

You may try:

lst1 <- list(list(list(c(14L, 13L, 5L, 4L, 9L), c(14L, 16L, 13L, 2L)),
list(c(3L, 2L, 7L, 1L, 8L), c(1L, 9L, 4L, 8L, 7L, 3L, 5L,
2L), c(4L, 2L, 1L))), list(list(c(7L, 14L, 15L, 4L, 3L),
c(10L, 12L, 6L, 1L)), list(c(5L, 8L, 10L, 3L, 4L), c(9L,
4L, 2L, 8L, 5L, 7L, 3L, 1L), c(1L, 4L, 3L))))

lapply(lst1,function(x) lapply(x,function(y) lapply(y,function(z) sum(z))))

A.K.

I have a list that has a header with the following information.
List of 6
\$ :List of 3
..\$ :List of 48

The structure of the list from the 1st to 4th looks like this.
[[1]]
[[1]][[1]]

[[2]]
[[2]][[2]]

[[3]]
[[3]][[3]]

[[4]]
[[4]][[4]]

I want to apply a function to the entire list but am unable to do it. How can one apply such a function to the list?

```