Created
August 2, 2016 12:49
-
-
Save yannabraham/d71fc120085613afbe2b3e1a312c071d to your computer and use it in GitHub Desktop.
Using tapply to return multiple values as an array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data(warpbreaks) | |
my.summary <- function(vals) { | |
return(c(avg=mean(vals),sd=sd(vals))) | |
} | |
sum.warpbreaks <- with(warpbreaks,tapply(breaks,list(tension,wool),my.summary)) | |
sum.warpbreaks <- array(unlist(sum.warpbreaks), | |
dim=c(length(sum.warpbreaks[1,1][[1]]),dim(sum.warpbreaks)), | |
dimnames=c(summary=list(names(sum.warpbreaks[1,1][[1]])), | |
dims=dimnames(sum.warpbreaks)) | |
) | |
sum.warpbreaks |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment