使用R中的List重新格式化数据框

时间:2011-04-14 17:55:36

标签: r

Helo,我正在尝试重新整形R中的data.frame,使每一行重复使用与列表不同的值,然后下一行将从列表的第二个条目的不同值重复。

列表被调用,wrk,dfx是我想要重塑的数据帧,而listOut就是我想要的结果。 非常感谢你的帮助。

> wrk
[[1]]
 [1] "41"  "42"  "44"  "45"  "97"  "99"  "100" "101" "102"
[10] "103" "105" "123" "124" "126" "127" "130" "132" "135"
[19] "136" "137" "138" "139" "140" "141" "158" "159" "160"
[28] "161" "162" "163" "221" "223" "224" ""   

[[2]]
 [1] "41"  "42"  "44"  "45"  "98"  "99"  "100" "101" "102"
[10] "103" "105" "123" "124" "126" "127" "130" "132" "135"
[19] "136" "137" "138" "139" "140" "141" "158" "159" "160"
[28] "161" "162" "163" "221" "223" "224" ""  

>dfx
  projectScore highestRankingGroup
1        0.8852                   1
2        0.8845                   2

>listOut
  projectScore highestRankingGroup    wrk
1        0.8852                   1    41
2        0.8852                   1    42
3        0.8852                   1    44
4        0.8852                   1    45
5        0.8852                   1    97
6        0.8852                   1    99
7        0.8852                   1   100
8        0.8852                   1   101
...
35       0.8845                   2    41
36       0.8845                   2    42
37       0.8845                   2    44
38       0.8845                   2    45
39       0.8845                   2    98
40       0.8845                   2    99
41       0.8845                   2   100

3 个答案:

答案 0 :(得分:2)

如何使用dfx ed cbind复制unlistwrk行:

listOut <- cbind(
    dfx[rep(seq_along(wrk), sapply(wrk, length)), ],
    wrk = unlist(wrk)
)

答案 1 :(得分:2)

怎么样:

如果wrk包含简单的向量,例如:

> szs<-sapply(wrk, length)

> fulldfr<-do.call(c, wrk)    

> listOut<-cbind(dfx[rep(seq_along(szs), szs),], fulldfr)

如果wrk包含数据帧:

> szs<-sapply(wrk, function(dfr){dim(dfr)[1]})

> fulldfr<-do.call(rbind, wrk)

> listOut<-cbind(dfx[rep(seq_along(szs), szs),], fulldfr)

答案 2 :(得分:1)

怎么样:

expand.grid(dfx$projectScore, dfx$highestRankingGroup, wrk[[1]])

编辑: 也许你可以详细说明一下,因为这似乎有效:

a <- c("41","42","44","45","97","99","100","101","102","103","105", "123","124","126","127","130","132","135","136","137","138","139","140","141","158","159","160","161","162","163","221","223","224")
wrk <-list(a, a)
dfx <- data.frame(projectScore=c(0.8852, 0.8845), highestRankingGroup=c(1,2))
listOut <- expand.grid(dfx$projectScore, dfx$highestRankingGroup, wrk[[1]])
names(listOut) <- c("projectScore", "highestRankingGroup", "wrk")
listOut[order(-listOut$projectScore,listOut$highestRankingGroup, listOut$wrk),]