[英]Programming in R: Using group_by to make a summary table
I am doing some data manipulation using group_by and summarize to get a summary table between two groups.我正在使用group_by进行一些数据操作并汇总以获取两组之间的汇总表。 Can't get it to work.无法让它工作。 Please let me know what went wrong and if there is a better code for this.请让我知道出了什么问题以及是否有更好的代码。 Thanks!谢谢!
This is my table before.这是我以前的桌子。
table_clean2 %>% group_by(member_casual) %>%
summarize(number_trips = count(member_casual),
duration_min = min(duration),
duration_max = max(duration),
duration_total = sum(duration))
Perhaps instead of count()
, try n()
.也许不是count()
,而是尝试n()
。 I don't have your dataset to test this answer, so here is a minimal, reproducible example using the palmerpenguins dataset :我没有你的数据集来测试这个答案,所以这里是一个使用palmerpenguins 数据集的最小的、可重复的例子:
library(tidyverse)
library(palmerpenguins)
penguins %>%
na.omit() %>%
group_by(sex) %>%
summarise(number_trips = n(),
duration_min = min(body_mass_g),
duration_max = max(body_mass_g),
duration_total = sum(body_mass_g),
average_duration = sum(body_mass_g) / n())
#> # A tibble: 2 × 6
#> sex number_trips duration_min duration_max duration_total average_duration
#> <fct> <int> <int> <int> <int> <dbl>
#> 1 female 165 2700 5200 637275 3862.
#> 2 male 168 3250 6300 763675 4546.
Created on 2021-12-07 by the reprex package (v2.0.1)由reprex package (v2.0.1) 于 2021 年 12 月 7 日创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.