pass grouped dataframe to own function in dplyr

后端 未结 1 1688
闹比i
闹比i 2021-01-12 08:18

I am trying to transfer from plyr to dplyr. However, I still can\'t seem to figure out how to call on own functions in a chained dplyr function.

I have a data frame

相关标签:
1条回答
  • 2021-01-12 08:38

    For those who get here from google. Let's say you wrote your own print function.

    printFunction <- function(dat) print(dat)
    df <- data.frame(a = 1:6, b = 1:2)
    

    As it was asked here

    df %>% 
        group_by(b) %>% 
        printFunction(.)
    

    prints entire data. To get dplyr print multiple tables grouped by, you should use do

    df %>% 
        group_by(b) %>% 
        do(printFunction(.))
    
    0 讨论(0)
提交回复
热议问题