r - 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
解决方案
您需要在动词across
内部使用dplyr
,例如mutate
or 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
推荐阅读
- python - 在嵌套的 RFECV 中缩放、然后调整超参数的管道。我究竟做错了什么?
- botframework - 如何从 MS Teams 消息扩展的 Bot 实现类将“类似调试”的消息记录到浏览器控制台?
- java - 部署到 Heroku 时 Java REST api 不起作用
- multithreading - 在 goroutine 中使用 io.Copy 时锁定
- typescript - 打字稿:对象中只允许大写键
- python - 使用 sklearn LinearRegression 时有没有办法约束预测值?
- redirect - Magento 2 检查是否有错误然后重定向到自定义 url
- c# - 我们如何在 C# 中简化这个 switch 表达式?
- r - 如何使用杂乱的数据列自动整理excel数据并导入R?
- react-native - 反应模块'YogaKit'的本机run-ios重新定义