- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试进行一些 dplyr 编程并且遇到了麻烦。我要 group_by
任意数量的变量(因此, across
),然后是 summarize
基于任意长度(但长度都相同)的向量:
map
中一样或
apply
语句,我想执行最终看起来像的代码:
data %>%
group_by(group_column) %>%
summarize(new_name_1 = function_1(column_1),
summarize(new_name_2 = function_2(column_2))
这是我想要的和迄今为止我最好的镜头的一个例子。我知道我可以使用
names
如果我使用 cross 来清理它们,但我不相信 cross 是正确的方法。最后,我将把它应用于相当大的数据帧,所以我不想计算额外的列。
mtcars %>%
group_by(across(c("cyl", "carb"))) %>%
summarise(across(c("disp", "hp"), list(mean = mean, sd = sd))) %>%
select(cyl, carb, disp_mean, hp_sd)
#> `summarise()` regrouping output by 'cyl' (override with `.groups` argument)
#> # A tibble: 9 x 4
#> # Groups: cyl [3]
#> cyl carb disp_mean hp_sd
#> <dbl> <dbl> <dbl> <dbl>
#> 1 4 1 91.4 16.1
#> 2 4 2 117. 24.9
#> 3 6 1 242. 3.54
#> 4 6 4 164. 7.51
#> 5 6 6 145 NA
#> 6 8 2 346. 14.4
#> 7 8 3 276. 0
#> 8 8 4 406. 21.7
#> 9 8 8 301 NA
我得到了什么
mtcars %>%
group_by(across(c("cyl", "carb"))) %>%
summarise(across(c("disp", "hp"), list(mean = mean, sd = sd)))
#> `summarise()` regrouping output by 'cyl' (override with `.groups` argument)
#> # A tibble: 9 x 6
#> # Groups: cyl [3]
#> cyl carb disp_mean disp_sd hp_mean hp_sd
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 4 1 91.4 21.4 77.4 16.1
#> 2 4 2 117. 27.1 87 24.9
#> 3 6 1 242. 23.3 108. 3.54
#> 4 6 4 164. 4.39 116. 7.51
#> 5 6 6 145 NA 175 NA
#> 6 8 2 346. 43.4 162. 14.4
#> 7 8 3 276. 0 180 0
#> 8 8 4 406. 57.8 234 21.7
#> 9 8 8 301 NA 335 NA
最佳答案
对于不同列的不同功能,一个选项是使用 collap
来自 collapse
library(collapse)
collap(mtcars, ~ cyl + carb, custom = list(fmean = 4, fsd = 5))
-输出
cyl disp hp carb
1 4 91.38 16.133815 1
2 4 116.60 24.859606 2
3 6 241.50 3.535534 1
4 6 163.80 7.505553 4
5 6 145.00 NA 6
6 8 345.50 14.433757 2
7 8 275.80 0.000000 3
8 8 405.50 21.725561 4
9 8 301.00 NA 8
match
动态生成索引
collap(mtcars, ~ cyl + carb, custom = list(fmean =
match('disp', names(mtcars)), fsd = match('hp', names(mtcars))))
tidyverse
, 一个选项是遍历感兴趣的列名和
map2
中的函数稍后加入
library(dplyr)
library(purrr)
library(stringr)
map2(c("disp", "hp"), c("mean", "sd"), ~
mtcars %>%
group_by(across(c('cyl', 'carb'))) %>%
summarise(across(all_of(.x), match.fun(.y),
.names = str_c("{.col}_", .y)), .groups = 'drop')) %>%
reduce(inner_join)
-输出
# A tibble: 9 x 4
cyl carb disp_mean hp_sd
<dbl> <dbl> <dbl> <dbl>
1 4 1 91.4 16.1
2 4 2 117. 24.9
3 6 1 242. 3.54
4 6 4 164. 7.51
5 6 6 145 NA
6 8 2 346. 14.4
7 8 3 276. 0
8 8 4 406. 21.7
9 8 8 301 NA
关于r - 如何以编程方式构建 dplyr 汇总语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68853709/
我怎样才能将 numberGrade 的值调高,如果它是 89.5,它会变成 90。numberGrade 被当作 double ,但将它设为 int 并不会向上或向下舍入。 public class
经过了漫长时间的移植和查询资料,得以解决一下嵌入式docker出现的问题,很多网上的资料全都是复制粘贴复制粘贴,找不到合适的解决方法让人很是苦恼,希望自己总结出的一些解决问题的经验给广大朋友减少一些
之前我是通过脚本来使用库的: 现在我使用 yarn 和 rollup 来构建带有下一个文件的项目。包.json: { "name": "maplib", "version": "1.0.0",
在 R 中,我正在尝试使用不同的窗口宽度对大向量(最多 400k 个元素)进行非常快速的滚动平均值,然后对于每个窗口宽度按每年的最大值汇总数据。下面的例子希望是清楚的。 我尝试了几种方法,到目前为止最
我想问一下我应该如何解决这个问题,因为我已经对这部分感到困惑和困惑。我已经使用这个命令全局安装了汇总 npm install --global rollup 但是,当我尝试运行“汇总”命令时,我应该期
我正在构建 javascript 库(更像是小部件),其中将包含一些 UI。我正在通过 javascript 向 DOM 添加 HTML 元素。要添加此 HTML,我有以下代码: async inse
我在显示一份报告时遇到了一些困难,该报告既可以将所有日期分组到月中,又可以对月中每天的“支出”求和。 我的 SQL 查询创建了这个:(检索所有日期) Date
我正在从事 Angular2 项目。我浏览了 Angular2 aot 文档并且能够生成 ngFactory 文件。我按照文档中的建议使用了 rollup js。我有一些非 es6 npm 包。我已经
我目前正在构建 Ionic 2-RC3 应用程序。但是自从我升级到 RC-1 后,我遇到了以下错误:(不确定它们是否保持不变,但你明白了)。 [15:16:17] rollup: Conflicti
Arabic, Egypt (ar_EG) -----------------------------阿拉伯语,埃及 Arabic, Israel (ar_IL) -----------------
我正在尝试汇总我的完全 es6 模块存储库,该存储库具有项目的本地导入/导出,以及对也是脚本或模块的依赖项的导入。 我也在尝试进行双重构建,通过汇总创建遗留的 iife 模块。 这仅适用于我的项目,没
我有一个由 DayTots 类对象组成的 VBA 集合(见下文) 我正在使用 For Each 遍历集合以创建一个 由汇总记录组成的新集合,基于日期 有什么方法可以用 Linq 做到这一点吗?我怀疑也
这是我第一次尝试理解/使用汇总。 我正在使用 this boilerplate因为它都是基于three.js,我也喜欢使用它。 到目前为止,我目前的(几乎肯定是不正确的)方法是: 从github下载样
我有两个 column_property 列,我想在 grandtotal 列中将它们加在一起。我希望能够根据 grandtotal 列进行排序和过滤。 如何对 subtotal 和 shipping
我收到以下错误消息: Error: Parse Error: Line 29: Unexpected token ILLEGAL 对应的代码行是 mobx 观察者装饰器: @observer clas
我真的坚持这一点,我真的很感激这方面的任何帮助。 目标是计算 Woocommerce 订单上每个类别中的项目数量,以便每个部分都可以以类别名称和产品数量为标题。例如: 汉堡 x 5 在此下方将是该订单
我正在从路由器收集传输数据;它提供每日,每月和每两分钟(间隔为120秒)的摘要。如果我在一天中(因此一个月中)重启路由器,则这些报告将不完整。但是,我仍然会得到间隔数据,并且可以对引导前后的记录进行汇
假设我有一个像这样的数据框: a b c d e f 1. 1 5 5 9 2 3 2. 4 7 3 1 4 6 3. 2 3 8 9
假设我有一个记录列表,我想通过取中位数来总结它。更具体地说,说我有 data Location = Location { x :: Double, y :: Double } 我有一个测量列表,我想将
我刚刚开始使用 AngularJS。我需要从 AngularJS 的书中升级这个购物车示例,以便所有 (items.price*item.quantity) 的总数显示在页面底部。实现它的推荐方法是什
我是一名优秀的程序员,十分优秀!