2013-03-27 68 views
4

我有这样一个数据帧:如何选择性地将列转换为data.frame中的行?

a1 a2 a3 a4 b c d1 d2 d3 d4 
[1] 0 0 1 0 0 0 0 1 0 0 
[2] 0 1 0 0 1 0 0 1 0 0 
... 

我想它下面转换,我想重塑可能没有用处在这里,你会是最好的方法?

a b c d 
[1] 0 0 0 0  # a and d as a1 and d1 of first row 
[2] 0 0 0 1  # a and d as a2 and d2 of first row 
[3] 1 0 0 0  # a and d as a3 and d3 of first row 
[4] 0 0 0 0  # a and d as a4 and d4 of first row 
[5] 0 1 0 0  # a and d as a1 and d1 of second row 
[6] 1 1 0 1  # a and d as a2 and d2 of second row 
[7] 0 1 0 0  # a and d as a3 and d3 of second row 
[8] 0 1 0 0  # a and d as a4 and d4 of second row 
... 

谢谢。

回答

2

如果数据是真的只是这么简单,像下面会做

DF <- read.table(text = "a1 a2 a3 a4 b c d1 d2 d3 d4\n0 0 1 0 0 0 0 1 0 0\n0 1 0 0 1 0 0 1 0 0", 
    header = TRUE) 
a <- as.vector(t(DF[, c("a1", "a2", "a3", "a4")])) 
d <- as.vector(t(DF[, c("d1", "d2", "d3", "d4")])) 
b <- rep(DF[, "b"], each = 4) 
c <- rep(DF[, "c"], each = 4) 
result <- data.frame(a, b, c, d) 
result 
## a b c d 
## 1 0 0 0 0 
## 2 0 0 0 1 
## 3 1 0 0 0 
## 4 0 0 0 0 
## 5 0 1 0 0 
## 6 1 1 0 1 
## 7 0 1 0 0 
## 8 0 1 0 0 
+0

感谢完美分割,甚至尽管数据并不那么简单,但它的工作。 – 2013-03-27 16:40:47

2

这里有一个半柔性的方式做你想要做什么..这将打破,如果你的列数去上述9除非你使用前导零..我觉得它假定您的列进行排序(可以用yourdata <- yourdata[ , sort(names(yourdata)) ]完成),所有列由最长的列(在final.nrow计算)

x <- read.table(text = "a1 a2 a3 a4 b c d1 d2 d3 d4\n0 0 1 0 0 0 0 1 0 0\n0 1 0 0 1 0 0 1 0 0", 
    header = TRUE) 

# this assumes your data are reasonably structured 

# here's a way to construct the "a" column in your desired structure-- 
as.numeric(t(x[ , grepl("a" , names(x)) ])) 

# so let's find all the column names, without their numbers 
cols <- unique(gsub("[1-9]" , "" , names(x))) 

# look at all column headers 
cols 

# find the number of records in the final table 
final.nrow <- nrow(x) * max(as.numeric(gsub("[a-z]" , "" , names(x))) , na.rm = TRUE) 

# initiate an empty data frame 
y <- NULL 

# loop through each of your column names 
for (i in cols){ 
    curCol <- as.numeric(t(x[ , grepl(i , names(x)) ])) 

    # find the multiple 
    expanded.col <- data.frame(rep(curCol , each = final.nrow/length(curCol))) 

    if (is.null(y)) y <- expanded.col else y <- cbind(y , expanded.col) 
} 

# tack on the final names 
names(y) <- cols 

# look at the final result 
y 
+0

感谢您的详细解释。 – 2013-03-27 16:41:13