[R] mean of a column in a list of data frames

Rui Barradas ruipbarradas at sapo.pt
Sun Feb 5 13:20:13 CET 2017


Hello,

Try instead the following.

aList<- numeric(3)
for(i in 1:3){
       aList[i] <- mean(ownersList[[i]]$grossIncome)
}
aList

Hope this helps,

Rui Barradas


Em 05-02-2017 12:01, Brandon Payne escreveu:
> 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]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list