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 that the inp[inp$key==x,] is not the best way. Is there an obvious speed up i'm missing? I can do it in Hadoop (since the 10e6 dataset is actually already a rollup from a 2e9 row dataset) but I'm trying to improve my R.
Cheers, Mat