Skip to content

Instantly share code, notes, and snippets.

@mhermans
Created November 21, 2014 11:51
Show Gist options
  • Save mhermans/8407cbcf3dd3b67b64c5 to your computer and use it in GitHub Desktop.
Save mhermans/8407cbcf3dd3b67b64c5 to your computer and use it in GitHub Desktop.
Berkeken groepsgemiddelden in R
# Berkeken groepsgemiddelden in R
data(iris)
summary(iris)
str(iris)
typeof(iris$Petal.Length)
typeof(iris$Species)
class(iris$Petal.Length)
class(iris$Species)
# Ingebouwd: aggregate()
aggregate(Sepal.Length~Species, iris, mean)
# Ingebouwd: tapply()
tapply(iris$Sepal.Length, iris$Species, mean)
# Ingebouwd: ave()
ave(iris$Sepal.Length, iris$Species) # geeft een vector terug met gemiddeld per group voor elke obsv.
# via library plyr
library(plyr)
# geeft datatable terug met verschillende variabelen per aggregerende functie
ddply(iris,~Species, summarise,
min=min(Sepal.Length),
max=max(Sepal.Length),
mean=mean(Sepal.Length),
sd=sd(Sepal.Length))
# geeft dataset terug met nieuwe var "groep_gem" met gem. groepswaarde per obsv
ddply(iris, "Species", transform, groep_gem = mean(Sepal.Length))
# via library doBy
library(doBy)
summaryBy(Sepal.Length ~ Species, data=iris, FUN=mean)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment