R语言笔记——汇总数据
本个专题将介绍R语言的一些基本技能和实用技巧。本教程说明了如何汇总或汇总数据。
创建样本数据
> set.seed(1)
> data <- data.frame(X = paste("s", sample(1:3, 15, replace = TRUE), sep = ""),Y = ceiling(rnorm(15)), Z = rnorm(15), A = rnorm(15), B = rnorm(15))
通过分组变量X计算Z的均值
> dat1 = aggregate(Z ~ X, data=data, FUN=mean)
# X Z
# 1 s1 -0.9504295
# 2 s2 0.2601957
# 3 s3 0.1786968
通过2个分组变量计算Z的均
> dat2 = aggregate(Z~ X + Y, data=data, FUN=mean)
## X Y Z
## 1 s3 -2 -0.47815006
## 2 s1 0 -1.47075238
## 3 s2 0 0.62794591
## 4 s3 0 0.61982575
## 5 s1 1 -1.04392394
## 6 s2 1 -0.10755445
## 7 s3 1 0.16693329
## 8 s1 2 -0.05612874
## 9 s3 2 0.41794156
通过将变量X分组来计算Y和Z的均值
> dat3 = aggregate(cbind(Y,Z)~X, data=data, FUN=mean)
# X Y Z
# 1 s1 1.0 -0.9504295
# 2 s2 0.5 0.2601957
# 3 s3 0.4 0.1786968
通过分组变量X计算所有变量的均值
> dat4 = aggregate(.~X, data=data, FUN=mean)
根据条件连接文本
testDF <- data.frame(v1 = c(1,3,5,7,8,3,5,NA,4,5,7,9),
+ v2 = c(11,33,55,77,88,33,55,25,44,55,77,99) )
",") aggregate(v2 ~ v1, data = testDF, FUN=paste, sep=
# v1 v2
# 1 1 11
# 2 3 33, 33
# 3 4 44
# 4 5 55, 55, 55
# 5 7 77, 77
# 6 8 88
# 7 9 99
◆
◆
◆
◆
◆
◆
◆
◆
◆
◆
◆