【问题标题】:I R ,how to summarise numeric columns automaticaly. Thanks [duplicate]I R ,如何自动汇总数字列。谢谢[重复]
【发布时间】:2021-09-08 03:59:33
【问题描述】:

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

【问题讨论】:

  • df %&gt;% summarise(across(where(is.numeric), sum))

标签: r dplyr tidyverse across


【解决方案1】:

你需要在dplyr动词里面使用across,比如mutate或者summarize,那么你需要在.fns中定义你要应用的函数,我用意思是 作为您数据中的示例。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    • 1970-01-01
    • 2013-08-09
    • 2020-11-03
    • 2017-01-21
    • 1970-01-01
    相关资源
    最近更新 更多