Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 有没有更快的方法来创建加权分数的数据框架?_R_Dplyr - Fatal编程技术网

R 有没有更快的方法来创建加权分数的数据框架?

R 有没有更快的方法来创建加权分数的数据框架?,r,dplyr,R,Dplyr,我正试图找到一种更好更快的方法来整理由加权平均数组成的汇总统计表。使用dplyr总结并绑定行,我最终得到了这样一个表。这些数字是简单的平均值。平均值是为每组的每个因素计算的 数据帧:au.scores AU AUDIT CORC GOV PPS TMSC TRAIN 1 Group1 2.833333 2.000000 2.733333 2.000000 1.750000 2.333333 2 Group2 2.833333 0.

我正试图找到一种更好更快的方法来整理由加权平均数组成的汇总统计表。使用dplyr总结并绑定行,我最终得到了这样一个表。这些数字是简单的平均值。平均值是为每组的每个因素计算的

数据帧:au.scores

         AU    AUDIT     CORC      GOV      PPS     TMSC    TRAIN
1 Group1 2.833333 2.000000 2.733333 2.000000 1.750000 2.333333
2 Group2 2.833333 0.000000 2.733333 2.000000 1.750000 2.333333
3 Group3 1.833333 2.533333 2.466667 2.000000 2.500000 2.166667
4 Group4 3.000000 2.733333 2.200000 2.666667 1.583333 2.666667
5 Group5 2.625000 1.816667 2.533333 2.166667 1.895833 2.375000
在此之后,我需要得出一个加权分数,该分数将每个变量的元素以及组1和组2与组3、4和组5相结合。也就是说,总体而言,第1组为第1组+第4组+第5组,第2组为第2组+第4组+第5组,第3组为第3组+第4组+第5组

group1.overall <- data.frame(
  group1.gov = (au.scores[3, 4] * .30) * .33 + (au.scores[1, 4] * .30) * .33 +
    (au.scores[2, 4] * .30) * .33,
  group1.corc = (au.scores[3, 3] * .30) * .33 + (au.scores[1, 3] * .1) * .33 +
    (au.scores[2, 3] * .1) * .33,
  group1.tmsc = (au.scores[3, 6] * .30) * .33 + (au.scores[1, 6] * .30) * .33 +
    (au.scores[2, 6] * .30) * .33,
  group1.audit = (au.scores[3, 2] * .30) * .33 + (au.scores[1, 2] * .30) * .33 +
    (au.scores[2, 2] * .30) * .33,
  group1.pps = (au.scores[3, 5] * .30) * .33 + (au.scores[1, 5] * .30) * .33 +
    (au.scores[2, 5] * .30) * .33,
  group1.train = (au.scores[3, 7] * .30) * .33 + (au.scores[1, 7] * .30) * .33 +
    (au.scores[2, 7] * .30) * .33
)
问题 有没有更快的方法来创建总分的数据框架

差不多

Group_Num / Gov / Corc / Tmsc / Audit / PPS / Train / Overall
Group1 / 0.78 / 0.31 / 0.59 / 0.74 / 0.59 / 0.67 / <- sum these 
Group2 / 0.66 / 0.23 / 0.44 / 0.66 / 0.22 / 0.43 / <- sum these
Group3 / 0.12 / 0.55 / 0.22 / 0.33 / 0.11 / 0.55 / <- sum these
Group_Num/Gov/Corc/Tmsc/Audit/PPS/Train/total
第1组/0.78/0.31/0.59/0.74/0.59/0.67/
总的来说,Group1是Group1+Group4+Group5,Group2是Group2+Group4+Group5
第3组是第3组+第4组+第5组因子

group1.overall <- data.frame(
  group1.gov = (au.scores[3, 4] * .30) * .33 + (au.scores[1, 4] * .30) * .33 +
    (au.scores[2, 4] * .30) * .33,
  group1.corc = (au.scores[3, 3] * .30) * .33 + (au.scores[1, 3] * .1) * .33 +
    (au.scores[2, 3] * .1) * .33,
  group1.tmsc = (au.scores[3, 6] * .30) * .33 + (au.scores[1, 6] * .30) * .33 +
    (au.scores[2, 6] * .30) * .33,
  group1.audit = (au.scores[3, 2] * .30) * .33 + (au.scores[1, 2] * .30) * .33 +
    (au.scores[2, 2] * .30) * .33,
  group1.pps = (au.scores[3, 5] * .30) * .33 + (au.scores[1, 5] * .30) * .33 +
    (au.scores[2, 5] * .30) * .33,
  group1.train = (au.scores[3, 7] * .30) * .33 + (au.scores[1, 7] * .30) * .33 +
    (au.scores[2, 7] * .30) * .33
)
您对如何计算总分的描述不同于您对
group1.overall
的公式,后者使用group1% 解组() #重新排列结果并计算每组的总和 au分数加权百分比 聚集(组、分数、类别)%>% 差价(类别、分数)%>% 选择(集团、政府、CORC、TMSC、审计、PPS、列车)%>% 突变(总体=政府+公司治理委员会+财务管理委员会+审计+PPS+列车) #一个tibble:3×8 集团政府CORC TMSC全面审核PPS培训 1组1 0.7391999 0.39655 0.5176874 0.837375 0.6765001 0.7301250 3.897437 2组2 0.7391999 0.33055 0.5176874 0.837375 0.6765001 0.7301250 3.831437 3组3 0.7128000 0.41415 0.5919374 0.738375 0.6765001 0.7136251 3.847388
编辑根据OP的问题添加代码解释:


摘要中向量顺序的意义是什么 功能?c(1,0,0,1,1))*3*0.33和c(0,1,0,1,1))*3*0.33&& c(0,0,1,1,1))

前面的步骤已经在每个类别中按顺序排列了组,因此在
weighted.mean
函数中使用权重
c(1,0,0,1,1)
相当于计算组1、4和5的平均值,而根本不使用组2和3。同上
c(0,1,0,1,1)
=第2、4和5组的平均值,`c(0,0,1,1,1)=第3、4和5组的平均值。我发现这比手动指定每个组更容易阅读/错误检查,因为手动指定可以快速将组号隐藏在一堆文本中


由此得出的平均值等于(组和)/3,或(组和)*0.3333。。。在十进制中,因为1/3是循环小数。由于原始公式使用(组之和)*0.33(小数点后两位四舍五入),因此将平均值乘以
*3*0.33将产生相同的结果。如果您希望得到更精确的结果,可以完全省去
*3*0.33
部分。

您是否尝试过
data.table
。它非常快(比data.frame快得多),summary函数中向量顺序的意义是什么?c(1,0,0,1,1))*3*0.33&&c(0,1,0,1,1))*3*0.33&&c(0,0,1,1))?到目前为止,这看起来很棒。很抱歉,我花了这么长时间才回复。它看起来像权重参数,但我认为权重是在表(weight.table)中指定的,这太棒了。我完全理解它是如何在代码中实现的。我仍在脑海中反复思考它到底是如何工作的。:)我从来没有想过。如果我想让第4组和第5组独立,而没有任何奇怪的组合,我是否只需要将向量分别放到c(0,0,0,0,1,0)和c(0,0,0,0,1)上,然后继续执行其余的操作?我注意到arrange()似乎实际上不起作用。当我按照代码中的说明排列这些向量时,它们保持升序alpha。很奇怪。这没什么大不了的,我只是重新安排了tribble,现在它按预期工作。升序是预期的默认值。您可以在帮助部分了解更多信息
?dplyr::arrange
library(dplyr); library(tidyr); library(tibble)

# read in au.scores data frame
au.scores <- read.table(text = "AU    AUDIT     CORC      GOV      PPS     TMSC    TRAIN
Group1 2.833333 2.000000 2.733333 2.000000 1.750000 2.333333
Group2 2.833333 0.000000 2.733333 2.000000 1.750000 2.333333
Group3 1.833333 2.533333 2.466667 2.000000 2.500000 2.166667
Group4 3.000000 2.733333 2.200000 2.666667 1.583333 2.666667
Group5 2.625000 1.816667 2.533333 2.166667 1.895833 2.375000", header = T)

# create table of weights (these are dummy weights since there's insufficient details in the question)
weight.table <- tribble(
  ~AU, ~GOV, ~CORC, ~TMSC, ~AUDIT, ~PPS, ~TRAIN,
  "Group1",.30,.10,.30,.30,.30,.30,
  "Group2",.30,.10,.30,.30,.30,.30,
  "Group3",.30,.10,.30,.30,.30,.30,
  "Group4",.30,.30,.30,.30,.30,.30,
  "Group5",.30,.10,.30,.30,.30,.30
)

# arrange columns in au.scores to match order of columns in weight.table
au.scores <- au.scores %>% arrange(AU, GOV, CORC, TMSC, AUDIT, PPS, TRAIN)

# calculate weighted scores
au.scores.weighted <- au.scores[,-1] * weight.table[,-1]
au.scores.weighted$AU <- au.scores$AU

# calculate scores for each group
au.scores.weighted <- au.scores.weighted %>%
  gather(category, weighted.score, -AU) %>%
  group_by(category) %>%
  arrange(AU) %>%
  summarise(group1 = weighted.mean(weighted.score, c(1,0,0,1,1)) * 3 * 0.33,
            group2 = weighted.mean(weighted.score, c(0,1,0,1,1)) * 3 * 0.33,
            group3 = weighted.mean(weighted.score, c(0,0,1,1,1)) * 3 * 0.33) %>%
  ungroup()

# rearrange result & calculate overall sum for each group
au.scores.weighted <- au.scores.weighted %>%
  gather(group, score, -category) %>%
  spread(category, score) %>%
  select(group, GOV, CORC, TMSC, AUDIT, PPS, TRAIN) %>%
  mutate(Overall = GOV + CORC + TMSC + AUDIT + PPS + TRAIN)

# A tibble: 3 × 8
   group       GOV    CORC      TMSC    AUDIT       PPS     TRAIN  Overall
   <chr>     <dbl>   <dbl>     <dbl>    <dbl>     <dbl>     <dbl>    <dbl>
1 group1 0.7391999 0.39655 0.5176874 0.837375 0.6765001 0.7301250 3.897437
2 group2 0.7391999 0.33055 0.5176874 0.837375 0.6765001 0.7301250 3.831437
3 group3 0.7128000 0.41415 0.5919374 0.738375 0.6765001 0.7136251 3.847388