I have two tables that look like this:
TABLE 1 (2.497.968 rows) - each row contains an ID (specifically, it represents a firm) and a place (the municipality where the firm is located)
c1 (ID) c2 (PLACE)
r1 1 1
r2 2 1
r3 3 1
r4 4 2
r5 5 2
r6 6 3
...
TABLE 2 (4.884.605 rows) - each row assigns a distance to each combination of places
c1 (PLACE_ORIGIN) c2 (PLACE_DESTINATION) c3 (DISTANCE)
r1 1 1 0
r2 1 2 5
r3 1 3 8
r4 1 4 15
r5 2 1 5
r6 2 2 0
r7 2 3 7
...
I want to join them in order to create a square matrix where rows and columns contain the ID's from the first table, and the values are given by the distances assigned to each pair PLACE_ORIGIN-PLACE_DESTINATION as provided by the second table.
Can I do this in R, and possibly also in Stata?
EDIT: Here's the output matrix I need:
ID1 ID2 ID3 ID4 ID5 ID6
ID1 0 0 0 5 5 3
ID2 0 0 7 2 2 3
ID3 0 7 0 2 9 11
ID4 5 2 2 0 7 15
ID5 5 2 9 7 0 2
ID6 3 3 11 15 2 0
...