Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Say it in R with by, apply and friends
## Markus Gesmann, January 2012
## Please install the following R packages first:
## data.table, doBy, plyr, reshape, sqldf, e.g. via
## install.packages(c("data.table", "doBy", "plyr", "reshape", "sqldf"))
f <- function(x) x^2
sapply(1:10, f)
do.call("rbind", as.list(
by(iris, list(Species=iris$Species), function(x){
y <- subset(x, select= -Species)
apply(y, 2, mean)
}
)))
iris.x <- subset(iris, select= -Species)
iris.s <- subset(iris, select= Species)
aggregate(iris.x, iris.s, mean)
aggregate( . ~ Species, iris, mean)
apply(iris.x, 2, function(x) tapply(x, iris.s, mean))
sapply(split(iris.x, iris.s), function(x) apply(x, 2, mean))
library(plyr)
ddply(iris, "Species", function(x){
y <- subset(x, select= -Species)
apply(y, 2, mean)
})
ddply(iris, "Species", function(x) colMeans(subset(x, select= -Species)))
library(reshape)
cast(melt(iris, id.vars='Species'),formula=Species ~ variable,mean)
library(doBy)
summaryBy(Sepal.Length + Sepal.Width + Petal.Length + Petal.Width ~ Species, data=iris, FUN=mean)
library(sqldf)
sqldf("select Species, avg(Sepal_Length), avg(Sepal_Width),
avg(Petal_Length), avg(Petal_Width) from iris
group by Species")
library(data.table)
iris.dt <- data.table(iris)
iris.dt[,lapply(.SD,mean),by="Species",.SDcols=1:4]
apply(iris3, c(3,2), mean)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.