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]]
?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]]
Hi Brian,
Thanks for the suggestion, although the command is throwing an error as
such:
> bevs %>% group_by(name, sex, drink) %>% summarise( cost = sum(cost))
%>%
select(name, drink, cost, sex)
Error: unexpected input in "bevs %>% group_by(name, sex, drink) %>%
summarise( "
Your syntax is new to me so I'm not immediately clear on how to fix it; any
idea how?
Thanks again,
Jonathan
On Wed, Jul 29, 2015 at 11:07 PM, Brian Kreeger <brian.kreeger at
gmail.com>
wrote:
> ?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]]
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 femaleDoesn'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
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]]