Skip to content

Instantly share code, notes, and snippets.

@yannabraham
Created August 2, 2016 12:49
Show Gist options
  • Save yannabraham/d71fc120085613afbe2b3e1a312c071d to your computer and use it in GitHub Desktop.
Save yannabraham/d71fc120085613afbe2b3e1a312c071d to your computer and use it in GitHub Desktop.
Using tapply to return multiple values as an array
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