Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用R中的列表重新格式化数据帧_R - Fatal编程技术网

使用R中的列表重新格式化数据帧

使用R中的列表重新格式化数据帧,r,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" [

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
那么:

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),]
a怎么样:

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),]

a用
unlist
ed
wrk
复制
dfx
cbind
的行怎么样:

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

listOut用
unlist
ed
wrk
复制
dfx
cbind
的行怎么样:

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

expand.grid(dfx$projectScore, dfx$highestRankingGroup, wrk[[1]])
如果wrk包含简单向量,如示例中所示:

> szs<-sapply(wrk, length)

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

> listOut<-cbind(dfx[rep(seq_along(szs), szs),], fulldfr)
szs fulldfr列表szs fulldfr列表如何:

expand.grid(dfx$projectScore, dfx$highestRankingGroup, wrk[[1]])
如果wrk包含简单向量,如示例中所示:

> szs<-sapply(wrk, length)

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

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

>szs fulldfr listOut szs fulldfr listOut当
wrk
中的第一个列表中有34个元素时,
listOut的第33行是什么?谢谢大家的提醒,这是我的错误。
listOut的第33行是什么当
wrk
中的第一个列表中有34个元素时?谢谢你的提醒Davor,这是我的错误。这不会起作用,我没有空间扩展到所有可能的网格。该解决方案对测试用例有效,但expand.grid最适合于要生成唯一组合的情况。我需要一个通用的解决方案来应用于具有不同dfx长度、可变wrk[[I]]长度以及data.frame中大量其他内容的案例。这不起作用,我没有空间扩展到所有可能的网格。该解决方案确实适用于测试案例,但expand.grid最适合于需要生成唯一组合的案例。我需要一个通用的解决方案来应用于具有不同dfx长度、可变wrk[[I]]长度以及data.frame中大量其他内容的情况。