0

I have a list with 160 dataframes, that all have the same structure. Every dataframe corresponds to one country

Afghanistan <- data.frame(seq(1970, 2010, 10), c(20,30,30,40,10))
Albania <- data.frame(seq(1970, 2010, 10), c(10, 40, NA, 50, 20))
colnames(Afghanistan) <- (c("Year", "Value"))
colnames(Albania) <- (c("Year", "Value"))
List1 <- list(Afghanistan, Albania)

Every Dataframe's structure looks like this:

Year    Value
1970    20
1980    30
1990    30
2000    40
2010    10

How can I get the mean of the column "Value" for each dataframe in the list. I tried to use the lapply function, but I couldn't figure out how to do it correctly. This didn't work:

lapply(List1[[]][,2], mean, na.rm = T)

Or would it be better to union all dataframes to one single big dataframe and then use aggregate to get the mean for every country?

BeneGIS
  • 13
  • 6
  • When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jan 30 '18 at 20:21
  • `lapply(ListName, function(df) mean(df[,2]))`? I'm with MrFlick, though, it is much better (and preferred) to include relevant (but simplified) sample data with your question. – r2evans Jan 30 '18 at 20:24

1 Answers1

2

If you have a list like this:

my_list <- list(data.frame(year = c(2000:2003),
                           value = c(1:4)),
                data.frame(year = c(2000:2003),
                           value = c(5:8)))

You can use lapply() to loop through that list. Each x is the a data.frame and you can access columns using $:

lapply(my_list, function(x) {
  mean(x$value)
})

This will return

[[1]]
[1] 2.5

[[2]]
[1] 6.5

UPDATE after edit:

lapply(List1, function(x) {
  mean(x$Value, na.rm = TRUE)
})

Returns:

[[1]]
[1] 26

[[2]]
[1] 30
clemens
  • 6,653
  • 2
  • 19
  • 31
  • Thank you very much. I think I missunderstood the functioning of lapply. Now I think it's clear to me. – BeneGIS Jan 30 '18 at 20:51
  • You're welcome, I am glad it works! If you want, you can accept the answer to show your question was answered. – clemens Jan 30 '18 at 20:56