创建新行来计算基于 R 中的组的值之间的百分比变化?
Create new rows that calculates % change between values based on group in R?
我创建了一个 table,它按 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
我想创建新的 rows/table 来计算每个组值相对于每个置信水平字段的百分比变化(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)))
您可以使用 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))
我创建了一个 table,它按 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
我想创建新的 rows/table 来计算每个组值相对于每个置信水平字段的百分比变化(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)))
您可以使用 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))