我有一个数据框,每一行代表一系列学校
edu <- read.table(header=TRUE, text="Elem Mid High
e1 m1 h1
e2 m2 h2
e1 m2 h2
e3 m1 h1")
我想把它变成一个边缘列表
s1 s2
1 e1 m1
2 e2 m2
3 e1 m2
4 e3 m1
5 m1 h1
6 m2 h2
7 m2 h2
8 m1 h1
对于有向图(通过 igraph 包)。
这是我的做法:
e2m <- edu[,1:2]
m2h <- edu[,2:3]
colnames(e2m) <- c("s1", "s2")
colnames(m2h) <- c("s1", "s2")
schools <- rbind(e2m,m2e)
“学校”包含我想要的内容,但如果我想添加第四列(例如“Uni”),它是迭代的并且变得很麻烦。什么是矢量化的方式来做到这一点?