Aggregating values in a data frame based on key
问题 I've got a piece of aggregation code that works well enough but runs a bit slow against a data frame with 10e6 rows. I'm not that experienced in R so apologies for my cringe worthy code! I just want to do a basic roll up and sum of values for a common key... eg go from... key val 1 a 5 2 b 7 3 a 6 to... key val 1 a 11 2 b 7 the best i can manage is... keys = unique(inp$key) vals = sapply(keys, function(x) { sum(inp[inp$key==x,]$val) }) out = data.frame(key=keys, val=vals) I have this gut feel