根据外部变量从数据框创建矩阵

Creating matrix from data frame according to external variable

我正在尝试创建一个简单的国家单位矩阵,以及它们是否相互接壤。这个想法是有一个大矩阵,其中变量是国家名称,如果它们共享边界,则给它们 1 或 0。

例如,给定此数据集:

mydata <- fread( "country    border
Afghanistan     China        
Afghanistan     Iran        
Afghanistan     Pakistan       
Afghanistan     Tajikistan      
Afghanistan     Turkmenistan      
Afghanistan     Uzbekistan        
Aland_Islands   NA                 
Albania         Greece
Albania         Montenegro
Albania         North_Macedonia
Albania         Serbia
Algeria         Libya
Algeria         Mali
Algeria         Mauritania
Algeria         Morocco
Algeria         Niger
Algeria         Tunisia")

我想创建以下内容:

mydata <- fread( "Country Afghanistan China Iran Pakistan Tajikistan Turkmenistan Uzbekistan Greece Albania Montenegro
Afghanistan 0 1 1 1 1 1 1 0 0 0
China 1 0 0 0 0 0 0 0 0 0
Iran 1 0 0 1 0 1 0 0 0 0  
Pakistan 1 1 1 0 0 0 0 0 0 0       
")
Country     Afghanistan China Iran Pakistan Tajikistan Turkmenistan Uzbekistan Greece Albania Montenegro
Afghanistan           0     1    1        1          1            1          1      0       0          0
      China           1     0    0        0          0            0          0      0       0          0
       Iran           1     0    0        1          0            1          0      0       0          0
   Pakistan           1     1    1        0          0            0          0      0       0          0

由于您的数据已经有 data.table,请尝试转换为宽度,使用 'length' 作为聚合函数,并用 0 填充缺失的组合。

dcast( mydata, country ~ border, fun.aggregate = length, fill = 0 )