如何重组和折叠数据集?

时间:2012-10-27 19:46:29

标签: r

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

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

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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))