This is quite possibly a duplicate of either or both of these, if so apologies and I guess that would make it an outstanding burning issue.
pass grouped dataframe to own function in dplyr
Using plyr one could run something like this:
ddply(mtcars, .(cyl), function(x) table(x$am))
and get the nice output
> ddply(mtcars, .(cyl), function(x) table(x$am))
cyl 0 1
1 4 3 8
2 6 4 3
3 8 12 2
I still don't really get why ddply(mtcars, .(cyl), table(am))
doesn't work, but nevermind.
Is there a way to achieve the above in dplyr?
mtcars %>%
group_by(cyl) %>%
function(x) table(x$am)
Doesn't achieve the same results.
UPDATED QUESTION (leaving the above for historical purposes).
In hindsight, while the above is something I would like to do from time to time, I was more trying to get at functionality like this:
blah <- function(x) {
x$position <- 1:nrow(x)
x$count <- nrow(x)
return(x)
}
ddply(mtcars, .(cyl,am), function(x) blah(x))