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
mean of a column in a list of data frames
3 messages · Brandon Payne, Duncan Murdoch, Rui Barradas
On 05/02/2017 7:01 AM, Brandon Payne wrote:
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
lapply returns a list of the first columns, and mean() doesn't know what to do with that.
There must be a more idiomatic way to write this with map or apply.
Yes, take the mean inside lapply: lapply(ownersList, function(x) mean(x[[1]])) Not tested. Duncan Murdoch
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)
}
for loops don't have a useful value: it's always NULL. This would have
worked as
aList <- list()
for(i in 1:3){
aList[[i]] <- mean(ownersList[[i]]$grossIncome)
}
Duncan Murdoch
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.