首页 > 解决方案 > IR,如何自动汇总数字列。谢谢

问题描述

IR,如何自动汇总数字列。谢谢

library(tidyverse)
df <- tibble(label=LETTERS[1:8],
             x = rep(2:5, each = 2) / 2, 
             y = rep(2:3, each = 4) / 2)


df %>% sum(across(where(is.numeric))) #can't work
df %>% sum(where(is.numeric)) #can't work

标签: rdplyrtidyverseacross

解决方案


您需要在动词across内部使用dplyr,例如mutateor summarize,然后您需要定义要在其中应用的函数.fns,我在您的数据中使用mean作为示例。

df %>% summarize(across(.cols = where(is.numeric),.fns = mean))

# A tibble: 1 x 2
      x     y
  <dbl> <dbl>
1  1.75  1.25

推荐阅读