【发布时间】:2018-02-04 11:10:32
【问题描述】:
使用 dply::summarise 时如何保留其中一个分组名称?或者,是否有更好的方法来保留其中一个组名?我可能这样做的效率很低。
我有一个 data.frame (df):
dput(head(df, n = 20))
structure(list(file_src = c("CBG_EFD.xlsx", "CBG_EFD.xlsx", "CBG_EFD.xlsx",
"CBG_EFD.xlsx", "CBG_EFD.xlsx", "CBG_EFD.xlsx", "CBG_EFD.xlsx",
"CBG_EFD.xlsx", "CBG_EFD.xlsx", "CBG_EFD.xlsx", "CBG_EFD.xlsx",
"CBG_EFD.xlsx", "CBG_EFD.xlsx", "CBG_EFD.xlsx", "CBG_EFD.xlsx",
"CBG_EFD.xlsx", "CBG_EFD.xlsx", "CBG_EFD.xlsx", "CBG_EFD.xlsx",
"CBG_EFD.xlsx"), AU = c("CBD", "CBD", "CBD", "CBD", "CBD", "CBD",
"CBD", "CBD", "CBD", "CBD", "CBD", "CBD", "CBD", "CBD", "CBD",
"CBD", "CBD", "CBD", "CBD", "CBD"), BU = c("OAO", "Constr", "Retail",
"OAO", "Constr", "Retail", "OAO", "Constr", "Retail", "OAO",
"Constr", "Retail", "OAO", "Constr", "Retail", "OAO", "Constr",
"Retail", "OAO", "Constr"), CC = c("AUDIT", "AUDIT", "AUDIT",
"AUDIT", "AUDIT", "AUDIT", "CORC", "CORC", "CORC", "CORC", "CORC",
"CORC", "CORC", "CORC", "CORC", "CORC", "CORC", "CORC", "CORC",
"CORC"), CA_LVL = c("AUDIT01", "AUDIT01", "AUDIT01", "AUDIT02",
"AUDIT02", "AUDIT02", "CORC01", "CORC01", "CORC01", "CORC02",
"CORC02", "CORC02", "CORC03", "CORC03", "CORC03", "CORC04", "CORC04",
"CORC04", "CORC05", "CORC05"), Score = c(1, 1, 2, 1, 3, 3, 1,
3, 2, 2, 4, 2, 2, 3, 1, 4, 2, 3, 3, 2)), .Names = c("file_src",
"AU", "BU", "CC", "CA_LVL", "Score"), row.names = c(NA, -20L), class = c("tbl_df",
"tbl", "data.frame"))
定义 其中 AU 是一组五 (5) 个“组”,BU 是一组五十五 (55) 个单元,所有这些单元都属于五个 AU 中的一个。亲子关系。分数是一个原始数字 0-4。 Control_Category 是一个有六个(字符串值)的变量。
目前,我的代码被分解为脚本执行两个级别的分组和聚合分数以给出一个简单的平均值。我首先在 AU 级别进行分组,以获得给定组的所有单元各自类别 (CC) 的简单平均值。最后,我有五个 data.frames(cbg.au.stat.wide、cbd.au.stat.wide 等)。这些 dfs 表示给定组的所有单元中给定类别的平均分数。
# Group1 assessment unit scores
cbg.au.stat.wide <- df %>%
group_by(AU, CC) %>%
filter(AU == "CBG") %>%
summarise(avg = mean(Score, na.rm = TRUE)) %>%
dcast(AU ~ CC, value.var = "avg") %>%
print() # end chain
产生:
cbg.au.stat.wide
AU AUDIT CORC GOV PPS TMSC TRAIN
1 CBG 3 2.733333 2.2 2.666667 1.583333 2.666667
稍后,所有“AU 级别”数据帧都使用 dplyr::bind_rows 进行组合
au.avg.scores <- bind_rows(
bsa.au.stat.wide,bsg.au.stat.wide,cbd.au.stat.wide,
cbg.au.stat.wide,wmg.au.stat.wide)
au.avg.scores
AU AUDIT CORC GOV PPS TMSC TRAIN
1 BSA Admin 2.833333 2.000000 2.733333 2.000000 1.750000 2.333333
2 BSG 2.833333 0.000000 2.733333 2.000000 1.750000 2.333333
3 CBD 1.833333 2.533333 2.466667 2.000000 2.500000 2.166667
4 CBG 3.000000 2.733333 2.200000 2.666667 1.583333 2.666667
5 WMG 2.625000 1.816667 2.533333 2.166667 1.895833 2.375000
然后我执行类似的分组和总结活动。只有这一次,我不是在 AU 级别(父级),而是在每个类别(CC)的 BU 级别进行。因此,对于一个给定的 AU,我知道有一个 BU 平均分数表,其中包含他们的控制类别。
# Group1 business units by Control Category
cbg.bu.stat.wide <- df %>%
group_by(BU, CC) %>%
filter(AU == "CBG") %>%
summarise(avg = mean(Score, na.rm = TRUE)) %>%
dcast(BU ~ CC, value.var = "avg") %>%
print() # end chain
产生:
BU AUDIT CORC GOV PPS TMSC TRAIN
1 Capital Markets 3 3.2 1.6 4 1.00 3
2 EFD 4 2.6 1.6 3 1.75 3
3 Global Trade Solutions 3 2.4 3.4 1 2.00 2
4 Investigations 1 NA NA NA NA NA
我认为您会在此处的“BU”级别注意到“AU”级别已被删除。最后,我想将所有这些 BU 合并到一个大表中,显示 BU 和 AU 的来源
所以它最终会看起来像这样:
> bu.avg.scores
AU BU AUDIT CORC GOV PPS TMSC TRAIN
CBG Adherence 3.0 1.4 3.2 1 1.50 3.0
CBG CTR 2.0 2.8 2.0 4 1.50 2.5
CBG HRCU 3.5 1.8 3.0 1 2.25 1.5
CBD Investigations 2.0 NA NA NA NA NA
BSG ACH 2.0 0.0 2.0 4 1.50 2.5
【问题讨论】: