如何在前几列之后将数据框的列移动到行中?

时间:2013-02-12 02:27:42

标签: r dataframe

我有一个数据框,每行代表一系列学校

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”),它会反复出现并变得很麻烦。这样做的矢量化方法是什么?

4 个答案:

答案 0 :(得分:11)

这是一个可能的解决方案:

len <- seq_along(edu)
a <- head(len, -1)
b <- tail(len, -1)

data.frame(s1=as.character(unlist(edu[, a])), s2=as.character(unlist(edu[, b])))

答案 1 :(得分:6)

直接将OP的代码翻译成申请。这不是矢量化的:

do.call(rbind, lapply(seq(ncol(edu)-1), FUN=function(x){
  r <- edu[,x:(x+1)]
  colnames(r) <- c('s1', 's2')
  r
}

))

答案 2 :(得分:3)

使用@ Tyler的方法:

# assuming a new column added
edu$Uni <- as.factor(c("u1", "u2", "u1", "u1"))  

rows  <- nrow(edu)
total <- prod(dim(edu))  # ie: nrow(edu) * ncol(edu)  

X <- as.character(unlist(edu))
data.frame(s1=X[1:(total-rows)],  s2=X[(rows+1):total])

结果:

   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
9  h1 u1  <~~~ Added "Uni" column
10 h2 u2  <~~~ Added "Uni" column
11 h2 u1  <~~~ Added "Uni" column
12 h1 u1  <~~~ Added "Uni" column

答案 3 :(得分:2)

具有矩阵输出的替代方案,由igraph函数接受。

t(
  matrix(
   apply(edu,1,function(x) x[c(1,rep(2:(length(x)-1),each=2),length(x))]),
   nrow=2
        )
 )

结果:

     [,1] [,2]
[1,] "e1" "m1"
[2,] "m1" "h1"
[3,] "e2" "m2"
[4,] "m2" "h2"
[5,] "e1" "m2"
[6,] "m2" "h2"
[7,] "e3" "m1"
[8,] "m1" "h1"

并转换为图表:

> graph.edgelist(result)
IGRAPH DN-- 7 8 -- 
+ attr: name (v/c)