【问题标题】:Using summarise_at with mean and multiplying the result R使用 summarise_at 与平均值并乘以结果 R
【发布时间】:2020-10-21 22:45:31
【问题描述】:

我需要通过计算平均值来总结一些列并将结果乘以 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

我做错了什么?

【问题讨论】:

  • dplyr 1.0.0: test %&gt;% summarise(across(everything(), ~mean(.) * 100))

标签: r dplyr summarize


【解决方案1】:

例如

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

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

【讨论】:

    猜你喜欢
    • 2021-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多