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

I R ,how to summarise numeric columns automaticaly. Thanks

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

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

您需要在dplyr动词中使用across,例如mutatesummarize,然后您需要在[中定义要应用的功能=15=],我在你的数据中使用了 mean 作为示例。

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

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