R - 归一化值(基于某些值)

R - Normalize values (based on some of the values)

我想规范化 R 中的数据,但不是在特定范围内(例如 0 到 1)。我有一个 table 如下所示:

 benchmark  |   technique     | ipc
correlation | no_compression  | 0.5
correlation | compression-bdi | 0.6
  trisolv   | no_compression  | 0.6
  trisolv   | compression-bdi | 0.7

我希望每个基准的 no_compression IPC 值为 1。特定基准的其余技术将基于该 no_compression 值。因此,例如,相关的 compression-bdi 的 IPC 值为 1.2。

有什么功能可以使用吗?我只能找到规范化到某个范围的提及。

使用dplyr

df %>% 
  group_by(benchmark) %>% 
  mutate(ipc_standardized = ipc / ipc[technique == 'no_compression'])
# A tibble: 4 x 4
# Groups:   benchmark [2]
  benchmark   technique         ipc ipc_standardized
  <chr>       <chr>           <dbl>            <dbl>
1 correlation no_compression    0.5             1   
2 correlation compression-bdi   0.6             1.2 
3 trisolv     no_compression    0.6             1   
4 trisolv     compression-bdi   0.7             1.17

或使用基数 R:

df$ipc_standarized <- unlist(lapply(
  split(df, df$benchmark), 
  function(.) .$ipc / .$ipc[.$technique == 'no_compression'])
)

您也可以使用 match 其中 returns 第一个匹配项的索引来查找 "no_compression" technique

library(dplyr)

df %>%
   group_by(benchmark) %>%
   mutate(ipc = ipc/ipc[match('no_compression', technique)])

#  benchmark   technique         ipc
#  <fct>       <fct>           <dbl>
#1 correlation no_compression   1   
#2 correlation compression-bdi  1.2 
#3 trisolv     no_compression   1   
#4 trisolv     compression-bdi  1.17

使用 data.table 就是

library(data.table)
setDT(df)[, ipc := ipc/ipc[match('no_compression', technique)], benchmark]