I want to create a pair-wise average of price of commodities produced by countries. My data looks like this
df <- data.frame(country = c("US; UK; FI", "CN; IT; US; GR", "UK; US"),
product_id = c(1, 2, 3),
price = c(300, 500, 200))
I want to transform the data to create average of price between dyads of two countries. Something like this:
Ctr_1 Ctr_2 Avg_Price
US UK 250
US FI 300
US CN 500
US IT 500
UK FI 300
UK US 250
CN IT 500
CN US 500
CN GR 500
IT CN 500
IT US 500
IT GR 500
GR CN 500
GR IT 500
GR US 500
I tried changing the data to long form.
library(data.table)
setDT(df)
df1 <- df[, .(country = unlist(strsplit(country, "; "))), by = .(product_id)]
But didn't know how to proceed from here. Any help would be really appreciated. In fact, there is a year variable as well, and the idea is to aggregate pair-wise per year to create a panel dataset.