I have a data frame that looks something like:
df <- data.frame(resource = c("gold", "bronze", "gold", "silver", "silver", "gold", "gold", "silver"), price = (c(10, 15, 20, 12, 12, 10, 10, 15)), extraction = c(100, 200, 50, 200, 250, 100, 50, 50))
r p e
1 gold 10 100
2 bronze 15 200
3 gold 20 50
4 silver 12 200
5 silver 12 250
6 gold 10 100
7 gold 10 50
8 silver 15 50
I would like to collapse this dataset by resource, such that I have one variable that counts total extraction volume and as many extra variables as there are unique prices of the resource. Additionally I would like to have another variable that, for each unique price, counts how many observations were valued at this price.
This would look something like:
ID r total_extr. price1 n_price1 price2 n_price2
1 gold 300 10 3 20 1
2 silver 500 12 2 15 1
3 bronze 200 15 1 NA NA
Ideally, prices would be ascending or descending (in my dataset, there are more than two different prices per group).
The first 50 rows of my original dataset are:
structure(list(extraction = c(NA, NA, 3800, 5000, 3800, 3800, 3800, 3800, 3800, 3800, 3800, 3800, 3800, 3800, 660, 3800, 125,
3800, 3800, 660, 100, 3800, 40950, 250, 250, 150000, 35000, NA,
1e+05, 53000, NA, 225000, NA, 260000, 260000, NA, 260000, NA,
260000, NA, NA, 260000, 260000, 260000, 260000, 40, 523, NA,
NA, 523), price = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 27226012, NA, 21677.578125, NA, NA, 21047.84765625,
15398.7431640625, NA, 12181.1640625, 11378.0888671875, 11137.2998046875,
0.326765239238739, 0.326765239238739, 0.326765239238739, 0.352094233036041,
0.352094233036041, 0.307463765144348, 0.307463765144348, 0.280774921178818,
0.280774921178818, 0.240696549415588, 0.240696549415588, 0.168027445673943,
0.168027445673943, 0.144999995827675, 0.144999995827675, 0.131485313177109,
0.131485313177109, 0.129491910338402, 0.103749454021454, 0.14696241915226,
473.7353515625, NA, NA, NA, NA), resource = c("salt", "salt",
"natural gas", "natural gas", "natural gas", "natural gas", "natural gas",
"natural gas", "natural gas", "natural gas", "natural gas", "natural gas",
"natural gas", "natural gas", "tin", "natural gas", "tin", "natural gas",
"natural gas", "tin", "tin", "natural gas", "gold", "gold", "gold",
"diamond", "diamond", "diamond", "diamond", "diamond", "diamond",
"diamond", "diamond", "diamond", "diamond", "diamond", "diamond",
"diamond", "diamond", "diamond", "diamond", "diamond", "diamond",
"diamond", "diamond", "diamond", "natural gas", "natural gas",
"natural gas", "natural gas")), row.names = c(NA, 50L), class = "data.frame")