R group by aggregate

后端 未结 3 1112
南笙
南笙 2021-01-22 10:37

In R (which I am relatively new to) I have a data frame consists of many column and a numeric column I need to aggregate according to groups determined by another column.

<
3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-22 11:25

    Using base R:

    df <- transform(df, Min = ave(Price, SessionID, FUN = min),
                        Max = ave(Price, SessionID, FUN = max))
    df
    #  SessionID   Price    Min     Max
    #1         1  624.99 624.99  697.99
    #2         1  697.99 624.99  697.99
    #3         1  649.00 624.99  697.99
    #4         7  779.00 710.00 2679.50
    #5         7  710.00 710.00 2679.50
    #6         7 2679.50 710.00 2679.50
    

    Since your desired result is not aggregated but just the original data with two extra columns, you want to use ave in base R instead of aggregate, which you would typically use if you wanted to aggregate the data by SessionID. (NB: AEBilgrau shows that you could also do it with aggregate with some additional matching.)

    Similarly, for dplyr, you want to use mutate instead of summarise because you dont want to aggregate/summarise the data.

    Using dplyr:

    library(dplyr)
    df <- df %>% group_by(SessionID) %>% mutate(Min = min(Price), Max = max(Price))
    

提交回复
热议问题