I have a data frame in R defined as follows:
data frame:
col 1 col 2 col 3 col4
200 AIG 8.5 12
800 AIG 8.1 20.1
500 A1B 20 50.5
800 A1B 12 30
120 A2M 1.6 8.5
dat <- structure(list(col1 = c(200, 800, 500, 800, 120), col2 = structure(c(3L,
3L, 1L, 1L, 2L), .Label = c("A1B", "A2M", "AIG"), class = "factor"),
col3 = c(8.5, 8.1, 20, 12, 1.6), col4 = c(12, 20.1, 50.5,
30, 8.5)), .Names = c("col1", "col2", "col3", "col4"), row.names = c(NA,
-5L), class = "data.frame")
Then I'd like to collapse the rows by id (in this case the unique ids are A1G, A1B, A2M).
Col 1, I'd like to collapse it by adding the rows with the same id.
Col 2, I'd like to collapse it to each unique id
Col 3, I'd like to collapse it as follows, take col1*col3, add them, and then divide them by the sum of col1.
I.e., the A1G new row value should be (8.5*20+8.1*80)/(80+20). Aka the weighted average of column 3 weighted by the values of col1.
Col 4, I'd like to take the maximum value.
The resulting data frame should look like:
column 1 column 2 column 3 column 4
800+200=1000 AIG (8.5*200+8.1*800)/1000=8.18 max(12,20.1)=20.1
800+500=1300 AIB (20*800+12*500)/1300=16.9 max(50.5, 30)=50.5
120 A2M 1.6 8.5
Any suggestions?