[R] mean of a column in a list of data frames
Brandon Payne
payneb at post.bgu.ac.il
Sun Feb 5 13:01:07 CET 2017
I have a list of data frames,
ownersList <- list(exp2004owners,exp2005owners,
exp2006owners,exp2007owners,
exp2008owners,exp2009owners,
exp2010owners,exp2011owners,
exp2012owners,exp2013owners,
exp2014owners)
I want to take the mean of the first column $grossIncome.
I can access the first column with
lapply(ownersList, "[[", 1) ##works
But I can't take the mean of that.
mean(lapply(ownersList, "[[", 1)) ##not working
There must be a more idiomatic way to write this with map or apply.
ownersIncome<- c(mean(ownersList[[1]]$grossIncome),
mean(ownersList[[2]]$grossIncome),
mean(ownersList[[3]]$grossIncome),
mean(ownersList[[4]]$grossIncome),
mean(ownersList[[5]]$grossIncome),
mean(ownersList[[6]]$grossIncome),
mean(ownersList[[7]]$grossIncome),
mean(ownersList[[8]]$grossIncome),
mean(ownersList[[9]]$grossIncome),
mean(ownersList[[10]]$grossIncome),
mean(ownersList[[11]]$grossIncome))
I tried a for loop, which also didn't work.
aList<-
for(i in 1:3){
mean(ownersList[[i]]$grossIncome)
}
aList
[[alternative HTML version deleted]]
More information about the R-help
mailing list