0

I have a data set where I'm trying to find the mean, minimum and maximum mass of each gang, and my code is as follows:

 mass <- Gangs$Mass_kg
 mass.summary <- ddply(Gangs, ~Gang_affiliation, summarise,
  minimum = min(mass),
  mean = mean(mass),
  maximum = max(mass))

It outputs as:

   Gang_affiliation     minimum mean maximum
    18th Street          86      92   114
    Aryan Brotherhood    86      92   114      
    Black Guerillas      86      92   114

But what I really want is for every affiliation to have their own result.

   Gang_affiliation     minimum mean maximum
    18th Street          86      92   114
    Aryan Brotherhood    84      98   121      
    Black Guerillas      87      95   127

Can someone help me?

duman403
  • 1
  • 1

1 Answers1

0

You should be able to achieve this by

library(dplyr)
Gangs %>% group_by(Gang_affiliation) %>% summarise(minimum = min(mass),
                                                  mean = mean(mass)
                                                  maximum = max(mass))

I hope this helps!

smanski
  • 541
  • 2
  • 7
  • It returns with "Error in UseMethod("group_by_") : no applicable method for 'group_by_' applied to an object of class "c('double', 'numeric')" – duman403 Mar 26 '18 at 01:26
  • Is your dataset `Gangs`? Be sure to see my edit to my answer. – smanski Mar 26 '18 at 01:34
  • My data set is `Gangs` but now it has returned to my original post; all the results are duplicated – duman403 Mar 26 '18 at 01:39
  • Note that is question is a duplicate of [https://stackoverflow.com/questions/21982987/mean-per-group-in-a-data-frame](https://stackoverflow.com/questions/21982987/mean-per-group-in-a-data-frame) – smanski Mar 26 '18 at 01:46
  • Thanks. Any tips for using multiple functions in `aggregate()`? edit: Looks like its `list(FUN1, FUN2)`? – duman403 Mar 26 '18 at 01:50