创建新行,根据 R 中的组计算值之间的百分比变化?

时间:2021-02-10 04:24:38

标签: r dplyr rbind

我创建了一个表格,该表格按 3 种不同的方法进行分组,并显示每组对应的准确度级别 (%),如下所示:

# A tibble: 3 x 7
# Groups:   Group [3]
  Group    `LOW CONFIDENCE` `HIGH CONFIDENCE` PERFECT `MEDIUM CONFIDENCE` `NO MATCH` `VERY LOW CONFIDENCE`
  <fct>               <dbl>             <dbl>   <dbl>               <dbl>      <dbl>                 <dbl>
1 First_LV          0.416               0.262   0.138               0.135     0.0489              0.000226
2 JW                0.00496             0.531   0.159               0.234     0.0681              0.00250 
3 LV                0.0894              0.450   0.159               0.217     0.0695              0.0143 

我想创建新的行/表来计算每个组值之间相对于每个置信度字段的变化百分比(First_LV 和 JW 之间的变化百分比,First_LV 和 LV 之间的变化百分比,然后是 JW 和 LV 的低置信度,高信心等...)

我正在考虑做这样的事情,但似乎没有得到全貌:

percent <- function(x) 100 * c(NA * x[1], diff(x) / head(x, -1))
Percent_change_table <- data.frame(table2[1], t(apply(table2[-1], 1, percent)))

1 个答案:

答案 0 :(得分:1)

您可以使用 combn 创建 Group 值的组合。

do.call(rbind, combn(seq(nrow(table2)), 2, function(x) {
  data.frame(combination = paste0(table2$Group[x], collapse = '-'), 
             ((table2[x[1], -1] - table2[x[2], -1])/table2[x[1], -1]) * 100)
}, simplify = FALSE))

#   combination LOW_CONFIDENCE HIGH_CONFIDENCE   PERFECT MEDIUM_CONFIDENCE
#1  First_LV-JW       98.80769      -102.67176 -15.21739        -73.333333
#11 First_LV-LV       78.50962       -71.75573 -15.21739        -60.740741
#2        JW-LV    -1702.41935        15.25424   0.00000          7.264957

#    NO_MATCH VERY_LOW_CONFIDENCE
#1  -39.26380           -1006.195
#11 -42.12679           -6227.434
#2   -2.05580            -472.000

数据

table2 <- structure(list(Group = c("First_LV", "JW", "LV"), LOW_CONFIDENCE = c(0.416, 
0.00496, 0.0894), HIGH_CONFIDENCE = c(0.262, 0.531, 0.45), PERFECT = c(0.138, 
0.159, 0.159), MEDIUM_CONFIDENCE = c(0.135, 0.234, 0.217), NO_MATCH = c(0.0489, 
0.0681, 0.0695), VERY_LOW_CONFIDENCE = c(0.000226, 0.0025, 0.0143
)), class = "data.frame", row.names = c(NA, -3L))