简体   繁体   中英

dplyr - from group_by into own function

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.

https://stackoverflow.com/questions/28388211/in-r-package-dplyr-how-to-use-own-defined-function-to-summarise-each

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

Turning my and docendo's comments into an answer, this is what do() is for.

mtcars %>% group_by(cyl, am) %>% do(blah(.))
# same results as
plyr::ddply(mtcars, plyr::.(cyl, am), function(x) blah(x))
# same as plyr with no anonymous function in this case
plyr::ddply(mtcars, plyr::.(cyl, am), blah)

Because blah is taking in your full data frame (at least in terms of columns) and returning a data frame, you don't need the anonymous function call.

A lot is similar between dplyr and ddply , if you want to add columns, you use mutate , if you want to collapse grouping variables with aggregate functions, you use summarise . do is the dplyr equivalent of doing something else to each piece of data, but it needs to return a data frame.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM