[R] dplyr help
Brian Kreeger
brian.kreeger at gmail.com
Thu Jul 30 05:07:11 CEST 2015
dplyr solution:
bevs %>% group_by(name, sex, drink) %>% summarise(cost = sum(cost)) %>%
select(name, drink, cost, sex)
The last select statement puts the output in the column order you wanted in
your result.
I hope this helps.
Brian
On Wed, Jul 29, 2015 at 9:37 PM, Jon BR <jonsleepy at gmail.com> wrote:
> Hello,
> I've recently discovered the helpful dplyr package. I'm using the
> 'aggregate' function as such:
>
>
> bevs <- data.frame(cbind(name = c("Bill", "Mary"), drink = c("coffee",
> "tea", "cocoa", "water"), cost = seq(1:8), sex = c("male","female")));
> bevs$cost <- seq(1:8)
>
> > bevs
> name drink cost sex
> 1 Bill coffee 1 male
> 2 Mary tea 2 female
> 3 Bill cocoa 3 male
> 4 Mary water 4 female
> 5 Bill coffee 5 male
> 6 Mary tea 6 female
> 7 Bill cocoa 7 male
> 8 Mary water 8 female
> >
>
> > aggregate(cost ~ name + drink, data = bevs, sum)
> name drink cost
> 1 Bill cocoa 10
> 2 Bill coffee 6
> 3 Mary tea 8
> 4 Mary water 12
>
> My issue is that I would like to keep a column for 'sex', for which there
> is a 1:1 mapping with 'name', such that every time 'Bill' appears, it is
> always 'male'.
>
> Does anyone know of a way to accomplish this, with or without dplyr? The
> ideal command(s) would produce this:
>
> name drink cost sex
> 1 Bill cocoa 10 male
> 2 Bill coffee 6 male
> 3 Mary tea 8 female
> 4 Mary water 12 female
>
> I would be thankful for any suggestion!
>
> Thanks,
> Jonathan
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list