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

后端 未结 4 1047
一生所求
一生所求 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:21

    As far as I know, there isn't an exact way to do what you're asking, because the function you're using (fivenum) doesn't return data in a way that can be easily bound to columns from within the 'ddply' function. This is easy to clean up, though, in a programmatic way.

    Step 1: Perform the fivenum function on each 'Species' value using the 'ddply' function.

    data <- ddply(iris, .(Species), summarize, value=fivenum(Petal.Width))
    
    #       Species value
    # 1      setosa   0.1
    # 2      setosa   0.2
    # 3      setosa   0.2
    # 4      setosa   0.3
    # 5      setosa   0.6
    # 6  versicolor   1.0
    # 7  versicolor   1.2
    # 8  versicolor   1.3
    # 9  versicolor   1.5
    # 10 versicolor   1.8
    # 11  virginica   1.4
    # 12  virginica   1.8
    # 13  virginica   2.0
    # 14  virginica   2.3
    # 15  virginica   2.5
    

    Now, the 'fivenum' function returns a list, so we end up with 5 line entries for each species. That's the part where the 'fivenum' function is fighting us.

    Step 2: Add a label column. We know what Tukey's five numbers are, so we just call them out in the order that the 'fivenum' function returns them. The list will repeat until it hits the end of the data.

    Tukeys_five <- c("Min","Q1","Med","Q3","Max") 
    data$label <- Tukeys_five
    
    #       Species value label
    # 1      setosa   0.1   Min
    # 2      setosa   0.2    Q1
    # 3      setosa   0.2   Med
    # 4      setosa   0.3    Q3
    # 5      setosa   0.6   Max
    # 6  versicolor   1.0   Min
    # 7  versicolor   1.2    Q1
    # 8  versicolor   1.3   Med
    # 9  versicolor   1.5    Q3
    # 10 versicolor   1.8   Max
    # 11  virginica   1.4   Min
    # 12  virginica   1.8    Q1
    # 13  virginica   2.0   Med
    # 14  virginica   2.3    Q3
    # 15  virginica   2.5   Max
    

    Step 3: With the labels in place, we can quickly cast this data into a new shape using the 'dcast' function from the 'reshape2' package.

    library(reshape2)
    dcast(data, Species ~ label)[,c("Species",Tukeys_five)]
    
    #      Species Min  Q1 Med  Q3 Max
    # 1     setosa 0.1 0.2 0.2 0.3 0.6
    # 2 versicolor 1.0 1.2 1.3 1.5 1.8
    # 3  virginica 1.4 1.8 2.0 2.3 2.5
    

    All that junk at the end are just specifying the column order, since the 'dcast' function automatically puts things in alphabetical order.

    Hope this helps.

    Update: I decided to return, because I realized there is one other option available to you. You can always bind a matrix as part of a data frame definition, so you could resolve your 'aggregate' function like so:

    data <- aggregate(Petal.Width ~ Species, iris, function(x) summary(fivenum(x))) 
    result <- data.frame(Species=data[,1],data[,2])
    
    #      Species Min. X1st.Qu. Median Mean X3rd.Qu. Max.
    # 1     setosa  0.1      0.2    0.2 0.28      0.3  0.6
    # 2 versicolor  1.0      1.2    1.3 1.36      1.5  1.8
    # 3  virginica  1.4      1.8    2.0 2.00      2.3  2.5
    

提交回复
热议问题