R语言学习笔记(二十四):plyr包的用法

时间:2022-09-07 12:01:02

plyr 这个包,提供了一组规范的数据结构转换形式。

Input/Output list data frame array
list llply() ldply() laply()
data frame dlply() ddply() daply()
array alply() adply() aaply()

一个简单的例子

  • 普通做法
iris.set <- iris[iris$Species == "setosa", -5]
iris.versi <- iris[iris$Species == "versicolor", -5]
iris.virg <- iris[iris$Species == "virginica", -5]
## Apply
mean.set <- colMeans(iris.set)
mean.versi <- colMeans(iris.versi)
mean.virg <- colMeans(iris.virg)
## Combine
mean.iris <- rbind(mean.set, mean.versi, mean.virg)
rownames(mean.iris) <- c("setosa", "versicolor", "virginica")
mean.iris
Sepal.Length Sepal.Width Petal.Length Petal.Width
setosa 5.006 3.428 1.462 0.246
versicolor 5.936 2.770 4.260 1.326
virginica 6.588 2.974 5.552 2.026
  • plyr
library(plyr)
ddply(iris, .variables = "Species", .fun = function(df_sub){
colMeans(df_sub[, -5])
})
Species Sepal.Length Sepal.Width Petal.Length Petal.Width
1 setosa 5.006 3.428 1.462 0.246
2 versicolor 5.936 2.770 4.260 1.326
3 virginica 6.588 2.974 5.552 2.026
ddply(iris, .variables = "Species", .fun = function(df_sub) {
model <- lm(Petal.Width ~ Petal.Length, data = df_sub)
return(c(model$coefficients, R2 = summary(model)$r.squared))
})
Species (Intercept) Petal.Length R2
1 setosa -0.04822033 0.2012451 0.1099785
2 versicolor -0.08428835 0.3310536 0.6188467
3 virginica 1.13603130 0.1602970 0.1037537