2016-08-14 50 views
-3
Area RA RI WA WI NA NI 
3 3 1 4 2 2 1 
2 2 1 3 1 2 1 
3 2 1 3 2 2 1 
2 2 1 3 1 1 1 
2 2 1 3 2 1 1 
2 2 1 2 1 2 1 
2 3 1 2 1 2 1 
3 1 1 2 2 1 1 
2 2 1 1 1 2 1 
2 2 1 2 1 2 1 
3 1 1 3 1 1 1 

我想保留的第一列,每两列堆栈:选择列由统治,它们堆叠中的R

Area columan 1 Column 2 
3 3 1 
2 2 1 
3 2 1 
2 2 1 
2 2 1 
2 2 1 
2 3 1 
3 1 1 
2 2 1 
2 2 1 
3 1 1 
3 4 2 
2 3 1 
3 3 2 
2 3 1 
2 3 2 
2 2 1 
2 2 1 
3 2 2 
2 1 1 
2 2 1 
3 3 1 
3 2 1 
2 2 1 
3 2 1 
2 1 1 
2 1 1 
2 2 1 
2 2 1 
3 1 1 
2 2 1 
2 2 1 
3 1 1 

你的建议高度赞赏!

回答

1

我们得到使用循环逻辑指数(c(TRUE, FALSE)交替列子集化数据集,而不第一列(df1[-1]),unlist并与第一列cbind之后。

d1 <- data.frame(Area = df1[,1], column1 = unlist(df1[-1][ c(TRUE, FALSE)]), 
      column2 = unlist(df1[-1][c(FALSE, TRUE)])) 
row.names(d1) <- NULL 
head(d1) 
# Area column1 column2 
#1 3  3  1 
#2 2  2  1 
#3 3  2  1 
#4 2  2  1 
#5 2  2  1 
#6 2  2  1 
tail(d1) 
# Area column1 column2 
#28 2  2  1 
#29 2  2  1 
#30 3  1  1 
#31 2  2  1 
#32 2  2  1 
#33 3  1  1