在 R 中创建枢轴 table
Creating pivot table in R
我知道这个问题已经被问过很多次了,但我在过去 2 天里浏览了互联网,找不到创建数据透视表 table 或摘要 table 所需的帮助就像我可以用 excel 做的那样。我对 R 完全陌生,完全不识字,所以我发现 R 包中的大多数示例帮助文件太复杂了。
我有一个数据列表 ("assemblage"),看起来像这样
Phase class NISP
<chr> <chr> <int>
1 L Aves 11
2 L Fish 128
3 L Mammals 14
4 K Aves 63
5 K Fish 30
6 K Mammals 311
7 J Aves 170
8 J Fish 327
9 J Mammals 740
10 I Aves 45
# ... with 18 more rows
我已经成功地使用 dcast()
做了一个 summary/pivottable 像 table
pivot <-dcast(assemblage, Phase ~ class, fun.aggregate = sum, value.var = "NISP", margins = TRUE)
结果如下
Phase Aves Fish Indeterminate Mammals (all)
1 A 1 0 0 6 7
2 B 2 0 0 3 5
3 C 58 20 0 255 333
4 E 5 2 0 5 12
5 F 14 0 0 17 31
6 H 121 154 1 784 1060
7 I 45 110 0 149 304
8 J 170 327 0 740 1237
9 K 63 30 0 311 404
10 L 11 128 0 14 153
11 (all) 490 771 1 2284 3546
但是我现在想不通的是:
- 将(全部)替换为 "Total"
- 按以下顺序排列列 ("Phase","Mammals","Fish","Aves","Indeterminate","Total")
- 添加新列,每个阶段每只动物 class 的百分比(占行总数的百分比)。
下面的代码应该一步一步地完成所有事情。如果有任何不清楚的地方,请告诉我。
# make some data
df = data.frame(Phase = c(1, 1, 2, 2, 3),
Fish = floor(rnorm(5, 150)),
Mammal = floor(rnorm(5, 50)))
df$all = rowSums(df[, 2:3])
# 1 change name
names(df)[which(names(df) == 'all')] = 'Total'
# 2 - reverse Fish and Mammal
idx1 = 2:3 # columns to change
idx2 = 3:2 # new order of columns
df[, idx1] = df[, idx2]
names(df)[idx1] = names(df)[idx2]
# 3 - calculate percentages
idxT = 2:3 # column indices of interest
newColNames = paste('%', names(df)[idxT])
tmp = df[, idxT, drop = FALSE] / matrix(df["Total"], ncol = length(idxT))
colnames(tmp) = newColNames
df = cbind(df, tmp)
我知道这个问题已经被问过很多次了,但我在过去 2 天里浏览了互联网,找不到创建数据透视表 table 或摘要 table 所需的帮助就像我可以用 excel 做的那样。我对 R 完全陌生,完全不识字,所以我发现 R 包中的大多数示例帮助文件太复杂了。
我有一个数据列表 ("assemblage"),看起来像这样
Phase class NISP
<chr> <chr> <int>
1 L Aves 11
2 L Fish 128
3 L Mammals 14
4 K Aves 63
5 K Fish 30
6 K Mammals 311
7 J Aves 170
8 J Fish 327
9 J Mammals 740
10 I Aves 45
# ... with 18 more rows
我已经成功地使用 dcast()
做了一个 summary/pivottable 像 tablepivot <-dcast(assemblage, Phase ~ class, fun.aggregate = sum, value.var = "NISP", margins = TRUE)
结果如下
Phase Aves Fish Indeterminate Mammals (all)
1 A 1 0 0 6 7
2 B 2 0 0 3 5
3 C 58 20 0 255 333
4 E 5 2 0 5 12
5 F 14 0 0 17 31
6 H 121 154 1 784 1060
7 I 45 110 0 149 304
8 J 170 327 0 740 1237
9 K 63 30 0 311 404
10 L 11 128 0 14 153
11 (all) 490 771 1 2284 3546
但是我现在想不通的是:
- 将(全部)替换为 "Total"
- 按以下顺序排列列 ("Phase","Mammals","Fish","Aves","Indeterminate","Total")
- 添加新列,每个阶段每只动物 class 的百分比(占行总数的百分比)。
下面的代码应该一步一步地完成所有事情。如果有任何不清楚的地方,请告诉我。
# make some data
df = data.frame(Phase = c(1, 1, 2, 2, 3),
Fish = floor(rnorm(5, 150)),
Mammal = floor(rnorm(5, 50)))
df$all = rowSums(df[, 2:3])
# 1 change name
names(df)[which(names(df) == 'all')] = 'Total'
# 2 - reverse Fish and Mammal
idx1 = 2:3 # columns to change
idx2 = 3:2 # new order of columns
df[, idx1] = df[, idx2]
names(df)[idx1] = names(df)[idx2]
# 3 - calculate percentages
idxT = 2:3 # column indices of interest
newColNames = paste('%', names(df)[idxT])
tmp = df[, idxT, drop = FALSE] / matrix(df["Total"], ncol = length(idxT))
colnames(tmp) = newColNames
df = cbind(df, tmp)