我希望找到一种矢量化方法来从数据框中的多列中获取绝对最大值。
基本上有一个等效于 pmax 函数来获得绝对最大值。
test_df <- tibble(
some_identifier = c("apple", "tunafish", "turkey_sandwich"),
val_a = c(-1, 2, 0),
val_b = c(-3, 3, NA),
val_c = c(2, 3, 1)
)
# this is what abs_max column should be
test_df$abs_max <- c(-3, 3, 1)
test_df
# A tibble: 3 x 5
some_identifier val_a val_b val_c abs_max
<chr> <dbl> <dbl> <dbl> <dbl>
1 apple -1 -3 2 -3
2 tunafish 2 3 3 3
3 turkey_sandwich 0 NA 1 1
abs_max 列是我想要创建的。一个不太理想的解决方案可能是循环遍历每一行;但想联系以确定可能的更好方法。