如何在写入文件之前定义行的顺序? [英] How to define the order of rows before writing to file?

查看:24
本文介绍了如何在写入文件之前定义行的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已接受对我的问题how-to-save-the-edge-list-of-igraph-object-with-the-predefined-layout的回答。我在回答时使用了建议的代码:

edges <- as_edgelist(g2)
idx1 <- match(edges[, 1], V(g2)$name)
idx2 <- match(edges[, 2], V(g2)$name)
result <- cbind(data.frame(edges),
        mylayout[idx1, ],
        mylayout[idx2, ]
  )
names(result) <- c("V1", "V2", "V1_x", "V1_y", "V2_x", "V2_y")
write.csv(result, file = "test.csv", row.names = FALSE)

目前,我实现了相反的操作:从文件中读取数据,创建图形并绘制。

result <- as.data.frame(read.csv(file = "test.csv"), sep = ",", header = TRUE))
g <- graph_from_data_frame(result[1:2], directed = FALSE) 

from <- unique(result[c("V1", "V1_x", "V1_y")])
to   <- unique(result[c("V2", "V2_x", "V2_y")])

names(from) <- c("V", "Vx", "Vy")
names(to)   <- c("V", "Vx", "Vy")

但是,在绘制图形之前,我必须对mylayout矩阵进行额外的处理,因为我需要保持节点的位置:

tmp       <- merge(from, to, all = TRUE)
VV        <- unlist(tmp[1])
newlayout <- as.matrix(tmp[2:3], ncol = 2)
newlayout <- newlayout[match(names(V(g)), VV), ] 
plot(g, layout = newlayout)

我认为merge()函数有问题,因为它按字母顺序对行进行排序:1, 10, 11, 12以此类推:

> merge(from, to, all = TRUE)
    V       Vx        Vy
1   1 146.6092 -186.2981
2  10 135.5057 -209.2721
3  11 177.4670 -209.2720
4  12 148.0673 -178.0015
5   2 140.3174 -180.5523
6   3 145.9292 -210.6084
7   4 167.5780 -210.3411
8   5 166.7762 -185.4851
9   6 137.6438 -218.8938
10  7 172.1216 -219.6955
11  8 175.3288 -180.6742
12  9 175.0615 -188.1577

但我听从了不同的顺序

> match(names(V(g)), VV)
 [1]  1  6  7  8  5  9 10 11 12  2  3  4

问题。是否可以在写入文件之前指定行的顺序? 主要问题是我应该将该文件传递给JavaScript进行进一步处理。

编辑。这里是讨论order-data-frame-rows-according-to-vector-with-specific-order

具有mylayout布局和工作代码的g2 图的示例如下。

g2 <- upgrade_graph(structure(list(12, FALSE, c(1, 4, 5, 6, 7, 8, 9, 10, 11, 3, 2), c(0, 0, 1, 2, 3, 3, 1, 2, 0, 0, 1), c(
  0, 10, 9, 1, 2, 3, 4,
  5, 6, 7, 8
), c(0, 9, 1, 8, 10, 2, 6, 3, 7, 4, 5), c(
  0, 0, 1,
  2, 3, 4, 5, 6, 7, 8, 9, 10, 11
), c(
  0, 4, 7, 9, 11, 11, 11, 11,
  11, 11, 11, 11, 11
), list(
  c(1, 0, 1), structure(list(), .Names = character(0)),
  list(name = c(
    "1", "3", "4", "5", "2", "6", "7", "8", "9",
    "10", "11", "12"
  )), list()
)), class = "igraph"))

mylayout <-
structure(c(146.60916, 145.92917, 167.57799, 166.77618, 140.31737, 
137.64381, 172.12157, 175.32881, 175.06154, 135.50566, 177.46696, 
148.06731, -186.29814, -210.6084, -210.34111, -185.48505, -180.55231, 
-218.89375, -219.69554, -180.67421, -188.15775, -209.27205, -209.27203, 
-178.00151), .Dim = c(12L, 2L), .Dimnames = list(c("1", "3", 
"4", "5", "2", "6", "7", "8", "9", "10", "11", "12"), NULL))

plot(g2, layout = mylayout)

########################################################################################
edges <- as_edgelist(g2)
idx1 <- match(edges[,1], V(g2)$name)
idx2 <- match(edges[,2], V(g2)$name)
result <- cbind(data.frame(edges),
        mylayout[idx1, ],
        mylayout[idx2, ]
  )
names(result) <- c("V1", "V2", "V1_x", "V1_y", "V2_x","V2_y")
########################################################################################
## uncomment two lines if you want to use file test.csv
## write.csv(result, file = "test.csv", row.names = FALSE)
## result = as.data.frame(read.csv(file = "test.csv"), sep=",", header = TRUE))
########################################################################################

g <- graph_from_data_frame(result[1:2], directed=FALSE) 

from <- unique(result[c("V1","V1_x","V1_y")])
to   <- unique(result[c("V2","V2_x","V2_y")])

names(from) <- c("V", "Vx", "Vy")
names(to)   <- c("V", "Vx", "Vy")

newlayout <- matrix() 
tmp       <- merge(from, to, all=TRUE)
VV        <- unlist(tmp[1])
newlayout <- as.matrix(tmp[2:3], ncol=2)
newlayout <- newlayout[match(names(V(g)), VV),] 
plot(g, layout = newlayout)

推荐答案

我认为您不需要merge来制作tmp。可以在fromto上使用unique+rbind生成newlayout

newlayout <- as.matrix(unique(rbind(from, to))[-1])
plot(g, layout = newlayout)

这篇关于如何在写入文件之前定义行的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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