将 summarise_at 与均值相乘并乘以结果 R

Using summarise_at with mean and multiplying the result R

我需要通过计算平均值来汇总一些列并将结果乘以 100。

这个有效:

test <- tibble(a = c(0.1, 0.3, 0.5),
           b = c(0.33, 0.44, 0.42))
test %>% summarise(ma = mean(a, na.rm = TRUE) * 100,
                   mb = mean(b, na.rm = TRUE) *100)
     ma    mb
  <dbl> <dbl>
1    30  39.7

这也有效:

test_2 <- test %>% summarise_all(list(mean), na.rm = TRUE)

test_2 * 100

   a        b
1 30 39.66667

但是因为我有很多专栏,所以我不喜欢第一个。我也想在一个更大的管道中完成这一切(所以我不喜欢选项 2),所以我希望这样的事情会起作用:

test %>% summarise_all(list(mean * 100), na.rm = TRUE)

但事实并非如此:

Error in mean * 100 : non-numeric argument to binary operator

我做错了什么?

例如

test %>% summarise_all(list(function(x) 100*mean(x, na.rm=TRUE)))

另外,请注意 summarise_all 已被 dplyr v1.0.0 中的 across 功能取代。