R - 计算基于另一个数据框的组合指标

R - Calculating a combined metric for a dataframe based on another

我的数据框如下所示:

df = data.frame(Region=c(rep("NORDICS",1100),rep("DACH",900),rep("MED",1800),rep("CEE",15000),
                     rep("FRANCE",2000),rep("UK&I",2500)),
            Score=c(sample(seq(from = 1, to = 4, by = 1), size = 1100, replace = TRUE,prob = c(0.6,0.2,0.1,0.1)),
                 sample(seq(from = 1, to = 4, by = 1), size = 900, replace = TRUE,prob = c(0.3,0.3,0.2,0.2)),
                 sample(seq(from = 1, to = 4, by = 1), size = 1800, replace = TRUE,prob = c(0.8,0.1,0.05,0.05)),
                 sample(seq(from = 1, to = 4, by = 1), size = 15000, replace = TRUE,prob = c(0.2,0.2,0.2,0.4)),
                 sample(seq(from = 1, to = 4, by = 1), size = 2000, replace = TRUE,prob = c(0.9,0.05,0.03,0.02)),
                 sample(seq(from = 1, to = 4, by = 1), size = 2500, replace = TRUE,prob = c(0.9,0.05,0.03,0.02))))

数据框是按地区划分的单个分数的集合,其中每个观察值都是一个问题的单个分数(第 Score 列)。

问题的评分从 1 到 4。

基于此数据框,我从 Score 列中按区域计算 KPI。 KPI 是 12 的响应总和除以给定区域的响应总数。

我下面的代码按地区计算 KPI:

library(dplyr)

KPI_by_Region=df %>% group_by(Region) %>%
summarise(KPI = sum(Score %in% c(1,2))/n())

我的问题

仅使用 KPI_by_Region 数据框,其中包含各地区的 KPI 分数 -

Can I find out the KPI score for all regions combined, without having to run my code over the entire dataframe (df)?

这是否给出了您正在寻找的结果?

KPI_by_Region <- df %>%
  group_by(Region) %>%
  summarise(KPI = sum(Score %in% c(1,2))/n(), Count = n())

allRegionsKPI <- sum(KPI_by_Region$KPI * KPI_by_Region$Count) / sum(KPI_by_Region$Count)