If I have this list
set.seed(123)
thelist <- list(a=data.frame(x1=rnorm(10), x2=rnorm(10)),
b=data.frame(x1=rnorm(10), x2=rnorm(10)),
c=data.frame(x1=rnorm(10), x2=rnorm(10)))
And wanted to calculate the mean of each column within each list I could do so with the following code.
sapply(do.call("rbind",thelist),mean)
How could I calculate the standard deviation, again for each column within each list (a:c), as there is no equivalent function for sd (at least to my knowledge)?
Any suggests would be appreciated.
A basic R solution would be using sapply
twice.
For mean only it is:
t(sapply(thelist, sapply, mean))
Resulting in
x1 x2
a 0.074625644 0.2086220
b -0.424558873 0.3220446
c -0.008715537 0.2216860
If you want both:
my_summary <- function(x){
c(mean = mean(x), sd = sd(x))
}
as.data.frame(lapply(thelist, sapply, my_summary))
Resulting in:
a.x1 a.x2 b.x1 b.x2 c.x1 c.x2
mean 0.07462564 0.208622 -0.4245589 0.3220446 -0.008715537 0.2216860
sd 0.95378405 1.038073 0.9308092 0.5273024 1.082518163 0.8564451
First, I'd make it stackable by making the name into a column:
for (i in seq_along(thelist)) thelist[[i]]$dfname <- names(thelist)[i]
Then, stack and take means with data.table
:
require(data.table)
DT <- rbindlist(thelist)
DT[,lapply(.SD,mean),by=dfname]
which gives
dfname x1 x2
1: a 0.074625644 0.2086220
2: b -0.424558873 0.3220446
3: c -0.008715537 0.2216860
You might also consider the summary
function, though it's clunky here:
DT[,as.list(unlist(lapply(.SD,summary))),by=dfname]
# dfname x1.Min. x1.1st Qu. x1.Median x1.Mean x1.3rd Qu. x1.Max. x2.Min. x2.1st Qu. x2.Median x2.Mean x2.3rd Qu. x2.Max.
# 1: a -1.265 -0.5318 -0.07983 0.074630 0.37800 1.715 -1.9670 -0.32690 0.3803 0.2086 0.6505 1.7870
# 2: b -1.687 -1.0570 -0.67700 -0.424600 0.06054 1.254 -0.3805 -0.23680 0.4902 0.3220 0.7883 0.8951
# 3: c -1.265 -0.6377 -0.30540 -0.008716 0.56410 2.169 -1.5490 -0.03929 0.1699 0.2217 0.5018 1.5160
Finally, copying my old answer, you could make your own summary-stats function:
summaryfun <- function(x) list(mean=mean(x),sd=sd(x))
DT[,as.list(unlist(lapply(.SD,summaryfun))),by=dfname]
# dfname x1.mean x1.sd x2.mean x2.sd
# 1: a 0.074625644 0.9537841 0.2086220 1.0380734
# 2: b -0.424558873 0.9308092 0.3220446 0.5273024
# 3: c -0.008715537 1.0825182 0.2216860 0.8564451
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With