How can I use functions returning vectors (like fivenum) with ddply or aggregate?

后端 未结 4 1046
一生所求
一生所求 2020-12-07 01:28

I would like to split my data frame using a couple of columns and call let\'s say fivenum on each group.

aggregate(Petal.Width ~ Species, iris,          


        
4条回答
  •  一整个雨季
    2020-12-07 02:12

    You can use do.call to call data.frame on each of the matrix elements recursively to get a data.frame with vector elements:

    dim(do.call("data.frame",dfr))
    [1] 3 7
    
    str(do.call("data.frame",dfr))
    'data.frame':   3 obs. of  7 variables:
     $ Species            : Factor w/ 3 levels "setosa","versicolor",..: 1 2 3
     $ Petal.Width.Min.   : num  0.1 1 1.4
     $ Petal.Width.1st.Qu.: num  0.2 1.2 1.8
     $ Petal.Width.Median : num  0.2 1.3 2
     $ Petal.Width.Mean   : num  0.28 1.36 2
     $ Petal.Width.3rd.Qu.: num  0.3 1.5 2.3
     $ Petal.Width.Max.   : num  0.6 1.8 2.5
    

提交回复
热议问题