简体   繁体   中英

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. 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() . I don't have your dataset to test this answer, so here is a minimal, reproducible example using the palmerpenguins dataset :

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM