4

我有一个看起来像这样的数据集:

0 Var1 Var 2 Var3 Var4
1 2|1
2 1|2
3            1|2
4            2|1
5                  2|1
6                  2|1
7       1|2
8       1|2

我想向左折叠,使其看起来像这样:

0 G.F  G.F.1
1 2|1  Var1
2 1|2  Var1
3 1|2  Var2
4 2|1  Var2
5 2|1  Var3
6 2|1  Var3
7 1|2  Var4
8 1|2  Var4

我有一个涉及 reshape 包的一般想法,但到目前为止还没有找到一个优雅的解决方案。

4

2 回答 2

3

试试这个:

library(reshape)
foo = data.frame(Var1=c("2|1","1|2","","",""),Var2=c("","","","","1|2"))
foo 
#   Var1 Var2
# 1  2|1     
# 2  1|2     
# 3          
# 4          
# 5       1|2

foo = cbind("Row"=seq(nrow(foo)),foo)
foo = melt(foo, "Row")
setNames(foo[foo$value!="", c(3,2)], c("G.F","G.F.1"))
#    G.F G.F.1
# 1  2|1  Var1
# 2  1|2  Var1
# 10 1|2  Var2
于 2012-10-27T19:57:27.667 回答
2

坚持基地,这会让你到达你要去的地方:

#create data set
foo <- data.frame(Var1=c("2|1","1|2","","",""),Var2=c("","","","","1|2"),
    Var3=c("", "", "1|2","2|1","") ,Var4=c("","","", "1|2", ""))

#Split data frame columns into lists, counts lengths of each and put together
v <- lapply(seq_along(foo), function(i) foo[, i][foo[, i] != ""])
data.frame(G.F = unlist(v), G.F.1 = rep(colnames(foo), sapply(v, length)))

或类似地:

lens <- sapply(foo, function(x) sum(x != ""))
x <- as.matrix(foo)  
data.frame(G.F = x[x!= ""], G.F.1 = rep(colnames(foo), lens))
于 2012-10-27T20:07:45.657 回答