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?
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.