[R] dplyr help

Jon BR jonsleepy at gmail.com
Thu Jul 30 05:16:17 CEST 2015


David,
    I do appreciate your help, if not the dose of contempt.  I hope you
feel OK.

Thanks for the tips,
-Jonathan

On Wed, Jul 29, 2015 at 11:14 PM, David Winsemius <dwinsemius at comcast.net>
wrote:

>
> On Jul 29, 2015, at 7:37 PM, Jon BR wrote:
>
> > Hello,
> >    I've recently discovered the helpful dplyr package.  I'm using the
> > 'aggregate' function as such:
>
> The `aggregate` function is part of base-R:
>
> > 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?
>
> As pointed out you have not yet demonstrated any dplyr functions.
>
> > 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
>
> Doesn't this (glaringly obvious?) approach succeed?
>
> > aggregate(cost ~ name + drink+sex, data = bevs, sum)
>   name  drink    sex cost
> 1 Mary    tea female    8
> 2 Mary  water female   12
> 3 Bill  cocoa   male   10
> 4 Bill coffee   male    6
> >
>
>
> >
> > I would be thankful for any suggestion!
> >
> > Thanks,
> > Jonathan
> >
> >       [[alternative HTML version deleted]]
> >
> >
>
> Please learn to post in plain text.
>
> --
>
> David Winsemius
> Alameda, CA, USA
>
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list