dplyr summarise when function return is vector-valued?

前端 未结 2 798
庸人自扰
庸人自扰 2021-02-12 09:43

The dplyr::summarize() function can apply arbitrary functions over the data, but it seems that function must return a scalar value. I\'m curious if there is a reas

2条回答
  •  没有蜡笔的小新
    2021-02-12 10:13

    This is why I still love plyr::ddply():

    library(plyr)
    f <- function(z) setNames(coef(lm(x ~ y, z)), c("a", "b"))
    ddply(df, ~ group, f)
    #   group           a          b
    # 1     A   0.5213133 0.04624656
    # 2     B   0.3020656 0.01450137
    # 3     C   0.2189537 0.22998823
    

提交回复
热议问题