【问题标题】:Convert Matrix to table in R将矩阵转换为 R 中的表
【发布时间】:2017-03-22 07:06:16
【问题描述】:

我有一个网络图作为矩阵,通过 R 中的 csv 加载,它计算人与人之间的 SMS 消息的数量。例如:

NULL   john    mary    jim    bob    sue
john   0       5       2      0      1
mary   5       0       2      0      5
jim    2       2       0      8      10
bob    0       0       8      0      0
sue    1       5       10     0      0

我想将这个用于 gephi 的表格转换成这样的表格:

SOURCE    TARGET    WEIGHT
john      mary      5
john      jim       2
john      bob       0
john      sue       1
mary      jim       2
mary      bob       0
mary      sue       5
jim       bob       8
jim       sue       10
bob       sue       0

然后我可以在 gephi 中将其可视化并作为网络图进行使用。

是否可以在矩阵 ('m') 上使用快速 R 操作将其放入表中并将其导出为另一个 csv 文件?

谢谢:)

【问题讨论】:

    标签: r csv matrix


    【解决方案1】:
    library(reshape2)
    
    df <- melt(m) ##Assuming your data is a matrix. i.e. the people's names are the row and col names.
    colnames(df) <- c("Source","Target","Weight")
    

    【讨论】:

    • 你可以用melt(mat, varnames = c("Source", "Target"), value.name = "Weight")把它变成一个衬里
    • 如何只取矩阵的上半部分,这样我们就不会产生重复的关系?
    • 你想要一些类似的东西:df[as.integer(as.factor(df$Source)) &lt; as.integer(as.factor(df$Target)), ]
    猜你喜欢
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    • 2015-04-27
    • 2014-05-15
    • 1970-01-01
    • 2017-10-02
    • 2015-07-24
    • 2013-02-12
    相关资源
    最近更新 更多