将矩阵转换为R中的表 [英] Convert Matrix to table in R

查看:39
本文介绍了将矩阵转换为R中的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a network graph as a matrix, loaded through a csv in R, and it counts the number of SMS messages between people. e.g:

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的数据转置成一个像这样的表:

I want to transpose this for gephi into a table that works like this:

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中将其可视化并用作网络图.

Then I can visualise and play with it as a network graph in gephi.

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

Is there a quick R operation to use on the matrix ('m') to make it into the table and export it as another csv file?

谢谢:)

推荐答案

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")

这篇关于将矩阵转换为R中的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆