-3

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. enter image description here

table_clean2 %>% group_by(member_casual) %>% 
summarize(number_trips = count(member_casual),
          duration_min = min(duration),        
          duration_max = max(duration),
          duration_total = sum(duration))
Hayley
  • 17
  • 3
  • 1
    What does "can't get it to work" mean exactly? What is your input and what is your desired output? Please do not share code or data as images. We can't copy/paste that into R. Share your data in a [reproducible format](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). What exactly do you mean by "better" code? What criteria should be use to determine what's better? – MrFlick Dec 07 '21 at 04:02
  • I'm trying to get a summary table; with 1) column names [member_casual, number_trips, duration_min, duration_max, duration_total] and 2) one row of data for member, 3) one row of data for casual. – Hayley Dec 07 '21 at 04:35

1 Answers1

0

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)

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46
  • @ jared_mamrot Thank you for your reply and show me how to do a reproducible example. I'll do that for future questions for sure. – Hayley Dec 07 '21 at 21:20
  • 1
    You're welcome; see [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) / [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) / [Quick Intro to Reproducible Examples in R with reprex](https://www.r-bloggers.com/2020/03/quick-intro-to-reproducible-example-in-r-with-reprex/) for future questions. If you follow the instructions there you won't get downvoted (and I won't get downvoted for answering your 'insufficient' question) – jared_mamrot Dec 07 '21 at 21:51