I'm working on some bilateral trade data with each row consisting of the ID for exporter and importer, as well as their trade amount. I then want to map the trade amount of each row onto its corresponding cell in a matrix object which has the IDs of "exporter" and "importer" listed as "row" and "column" dimnames
.
I am wondering what will be an easier way to do this? Below is my current working code.
# import data
mat <- readRDS(url("https://www.dropbox.com/s/aj1607s975c5gf6/mat.rds?dl=1"))
head(mat, 10)
# import ID
id <- readRDS(url("https://www.dropbox.com/s/6weala2j0idb16i/id.rds?dl=1"))
# create matrix (there are a total of 161 possible IDs though not all of them appear on the data)
matrix <- matrix(rep( 0, len=161*161), nrow = 161)
dimnames(matrix) <- list(unique(id), unique(id))
# how can I fill the trade value (in mat[, 3]) into the corresponding cell on the matrix by match mat[, 1] and mat[, 3] on the dimnames(matrix)?