I have a list like this
list <- structure(list(`1` = structure(c(274L, 173L), .Dim = 2L, .Dimnames = structure(list(
c("2004", "2005")), .Names = ""), class = "table"), `2` = structure(73L, .Dim = 1L, .Dimnames = structure(list(
"2005"), .Names = ""), class = "table"), `3` = structure(c(334L,
365L, 366L, 365L, 365L, 365L, 366L, 365L, 287L), .Dim = 9L, .Dimnames = structure(list(
c("1990", "1991", "1992", "1993", "1994", "1995", "1996",
"1997", "1998")), .Names = ""), class = "table"), `4` = structure(139L, .Dim = 1L, .Dimnames = structure(list(
"2001"), .Names = ""), class = "table"), `5` = structure(71L, .Dim = 1L, .Dimnames = structure(list(
"2009"), .Names = ""), class = "table"), `6` = structure(77L, .Dim = 1L, .Dimnames = structure(list(
"1997"), .Names = ""), class = "table")), .Names = c("1",
"2", "3", "4", "5", "6"))
The first level of this list is a rising number. In the second level we have tables with years as column-names.
I want to build a sum of all elements of the second level where the column name is 2005. How would I do this?
You can use a combination of sapply
and [
.
> sum(sapply(list, `[`, "2005"), na.rm=TRUE)
[1] 246