[R] dplyr help

Jon BR jonsleepy at gmail.com
Thu Jul 30 04:37:56 CEST 2015


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]]



More information about the R-help mailing list