简体   繁体   English

R数据输出奇怪的格式化

[英]R data output weird formatting

I have a result of a function I wanna export from R to visualize it in Gephi. 我有一个想要从R导出以在Gephi中可视化它的函数的结果。

The result is 79.552 pairs listed like this 结果是这样列出的79.552对

[[79549]]
[1] 22 26

[[79550]]
[1] 41 26

[[79551]]
[1]34 26

[[79552]]
[1]25 26

I wan't the pairs to be exported so I have them like this in .csv 我不需要导出配对,所以我在.csv中有这样的配对

22, 26
41, 26
34, 26
25, 26

I'm using the write.table function like this, where kj.csv is my file and kj is my saved function. 我正在使用这样的write.table函数,其中kj.csv是我的文件, kj是我的保存函数。

write.table(kj, file = "kj.txt", append = FALSE, 
            quote = TRUE, sep = ",", eol = "\r\n",  
            na = "NA", dec = ".", row.names = TRUE, 
            col.names = TRUE, 
            qmethod = c("escape", "double"),fileEncoding = "")

A sample from my result .csv file looks like this (not the same numbers) 我的结果.csv文件中的示例如下所示(数字不同)

"c.22..26..483","c.41..26..596","c.34..26..543","c.25..26..773"

Can anyone help me? 谁能帮我?

You have a list object. 您有一个列表对象。 You could create a matrix and save it to file. 您可以创建一个矩阵并将其保存到文件中。 Here some sample data: 这里有一些示例数据:

kj <- list(c(1,2), c(2,3))

Now create the matrix by treating each entry of the list as a row: 现在,通过将列表的每个条目都视为一行来创建矩阵:

kj <- do.call(rbind, kj)

The matrix looks like this: 矩阵如下所示:

> kj                                                                                                                                                                                                                                                                           
     [,1] [,2]                                                                                                                                                                                                                                                                 
[1,]    1    2                                                                                                                                                                                                                                                                 
[2,]    2    3    

Save the matrix to file: 将矩阵保存到文件:

write.table(kj, "kj.txt")

The file kj.txt looks as follows: 文件kj.txt如下所示:

"V1" "V2"
"1" 1 2
"2" 2 3
write.csv(t(as.data.frame(kj)), "kj.txt", row.names=FALSE)

You might possibly be interested by Rgexf, a library for R to create network files in the gexf format. 您可能会对Rgexf感兴趣,Rgexf是R的库,用于以gexf格式创建网络文件。 This is a rich format that Gephi can read (it was actually created by the founders of Gephi). 这是Gephi可以读取的丰富格式(它实际上是Gephi的创建者创建的)。

Check: http://cran.r-project.org/web/packages/rgexf/index.html 检查: http : //cran.r-project.org/web/packages/rgexf/index.html

And a comparison of the gexf format with other ones, such as csv: 并将gexf格式与其他格式(例如csv)进行比较:

https://gephi.org/users/supported-graph-formats/ https://gephi.org/users/supported-graph-formats/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM