2

Suppose I have a nice little data frame

df <- data.frame(x=seq(1,5),y=seq(5,1),z=c(1,2,3,2,1),a=c(1,1,1,2,2))
df
##  x y z a
## 1 1 5 1 1
## 2 2 4 2 1
## 3 3 3 3 1
## 4 4 2 2 2
## 5 5 1 1 2

and I want to aggregate a part of it:

aggregate(cbind(x,z)~a,FUN=sum,data=df)
##  a x z
## 1 1 6 6
## 2 2 9 3

How do I go about making it programmatic? I want to pass:

  1. The list of variables to be aggregated cbind(x,z)
  2. The grouping variable a (I will be using it in several other parts of the program, so passing the whole thing cbind(x,z)~a is not helpful)
  3. The environment within which the things are happening

My starting point is

blah <- function(varlist,groupvar,df) {
# I kinda like to see what I am doing here 
cat(paste0(deparse(substitute(varlist)),"~",deparse(substitute(groupvar))),"\n")
cat(is.data.frame(df),"\n")
cat(dim(df),"\n")
# but I really need to aggregate this
return( aggregate(eval(deparse(substitute(varlist))~deparse(substitute(groupvar)),df),
FUN=sum,data=df) )
}

and it works halfway:

blah(cbind(x,z),a,df)
## [1] "cbind(x, z)~a"
## TRUE 
## 5 4
## Error in FUN(X[[i]], ...) : invalid 'type' (character) of argument 

So I am kind of able to build the character representation of the formula that I need, but putting it into aggregate() fails.

StasK
  • 1,525
  • 10
  • 21
  • 1
    `aggregate(df[c("x", "z")], df[c("a")], sum)` – d.b Sep 14 '17 at 16:49
  • Expanding on the answer by @d.b: `f = function(data, vars, group, fun) { aggregate(data[vars], data[group], fun)}` and then just run `f(data, c('x', 'z'), 'a', sum)` – tblznbits Sep 14 '17 at 16:53
  • OK. Care to convert this to an answer? That particular problem is solved, but there are other bits in the program where I'd still need to refer to `a` in the RHS of a formula though. – StasK Sep 14 '17 at 17:06
  • Before posting it as an answer, can you elaborate on how you envision using `a` on the RHS where replacing `a`, `data$a`, or `data['a']` with 'group', or `data[group]` would not work? – tblznbits Sep 14 '17 at 17:19
  • 1
    @brittenb, let me see if I stumble into that as I code the function that I need. Thanks for your help. – StasK Sep 14 '17 at 17:41

0 Answers0