This question already has an answer here:
I want to average the values by their IDs but not all ID's have the same number of values. How do I do this in R?
I have two columns ID and Value
ID Value
1000 0.51
1000 0.01
1001 0.81
1001 0.41
1001 0.62
1002 0.98
1002 0.12
1002 0.15
1003 0.12
... ...
You could use the package
dplyr
and the functionsummarise_each
:which gives you:
If you deal with large datasets this should be the most efficient way of doing this task.
Using
sqldf
:Output:
With
dplyr
, instead ofsummarise_each
as Cleb pointed out, we can just usesummarise
:Output:
You can try
by()
:or
aggregate()
:or using
data.table
(if you need fast calculation on large data sets):data