So I have a table structured like this
id V1 V2
101, 500, 1
101, 600, 1
102, 300, 0
102, 300, 0
102, 400, 0
102, 100, 1
103, 200, 0
103, 400, 0
104, 200, 1
And basically for each id, I want to calculate the mean of V1, and the sum of V2, so the new table should look like this
id V1 V2
101, 550, 2
102, 275, 1
103, 400, 0
104, 200, 1
If anyone can help out I'd really appreciate that.
We can use one of the aggregating functions for this kind of problems. Here, I use
dplyr
. Wegroup_by
'id' andsummarise
the 'V1' and 'V2' columns withmean
andsum
of those corresponding columns.Or another option is
data.table
. We convert the 'data.frame' to 'data.table' (setDT(df1)
), grouped by 'id', we get themean
andsum
of the columns.Or using
base R