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.
<
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))