将2列数据帧转换为对称二进制矩阵

时间:2017-12-08 23:26:52

标签: r

我试图在R中创建一个对称矩阵,如果列A = dataSet1和列B = dataSet2一起出现在同一行,那么在矩阵中设置为1,否则它为0.但是,我似乎无法理解R中的tcrossprod()函数。我已经能够使用表来制作一个不对称的矩阵,但是(对称/方形)矩阵没有运气。

我尝试过这个例子,但没有成功: Building a symmetric binary matrix

我的数据:

   structure(list(dataSet1 = c("g14773.t1", "g6302.t1", "g399.t1", 
"g15590.t1", "g2595.t1", "g149.t1"), dataSet2 = c("g6302.t1", 
"g14773.t1", "g482.t1", "g14053.t1", "g1006.t1", "g6302.t1")), .Names = c("dataSet1", 
"dataSet2"), row.names = c(NA, -6L), class = "data.frame")

每当我使用tcrossprod函数时,我会得到不正确的二进制文件并且缺少值:

 tcrossprod(table(head(Data)))
              dataSet2
  dataSet1    g14773.t1 g149.t1 g15590.t1 g2595.t1 g399.t1 g6302.t1
  g14773.t1         1       1         0        0       0        0
  g149.t1           1       1         0        0       0        0
  g15590.t1         0       0         1        0       0        0
  g2595.t1          0       0         0        1       0        0
  g399.t1           0       0         0        0       1        0
  g6302.t1          0       0         0        0       0        1 

有关如何将其变为对称矩阵的任何建议,如下所示:

structure(list(g14773.t1 = c(0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L), g6302.t1 = c(1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L), g399.t1 = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), g15590.t1 = c(0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L), g2595.t1 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L), g149.t1 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), g482.t1 = c(0L, 
0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L), g14053.t1 = c(0L, 0L, 0L, 1L, 
0L, 0L, 0L, 0L, 0L), g1006.t1 = c(0L, 0L, 0L, 0L, 1L, 0L, 0L, 
0L, 0L)), .Names = c("g14773.t1", "g6302.t1", "g399.t1", "g15590.t1", 
"g2595.t1", "g149.t1", "g482.t1", "g14053.t1", "g1006.t1"), class = "data.frame", row.names = c("g14773.t1", 
"g6302.t1", "g399.t1", "g15590.t1", "g2595.t1", "g149.t1", "g482.t1", 
"g14053.t1", "g1006.t1")) 

1 个答案:

答案 0 :(得分:1)

您可以将列df$dataSet1df$dataSet2转换为factor,指定其级别是两列中找到的值的并集。然后只需使用table

df$dataSet1 = factor(df$dataSet1,levels=union(df$dataSet1,df$dataSet2))
df$dataSet2 = factor(df$dataSet2,levels=union(df$dataSet1,df$dataSet2))
res = table(df)

Table将包含计数而不是0/1值,因此,例如,如果两个值一起找到两次,则矩阵中的值将为2。 如果这是一个问题,您可以添加:

res[res>0] = 1

结果:

           dataSet2
dataSet1    g14773.t1 g6302.t1 g399.t1 g15590.t1 g2595.t1 g149.t1 g482.t1 g14053.t1 g1006.t1
  g14773.t1         0        1       0         0        0       0       0         0        0
  g6302.t1          1        0       0         0        0       0       0         0        0
  g399.t1           0        0       0         0        0       0       1         0        0
  g15590.t1         0        0       0         0        0       0       0         1        0
  g2595.t1          0        0       0         0        0       0       0         0        1
  g149.t1           0        1       0         0        0       0       0         0        0
  g482.t1           0        0       0         0        0       0       0         0        0
  g14053.t1         0        0       0         0        0       0       0         0        0
  g1006.t1          0        0       0         0        0       0       0         0        0