开发者

reformatting data frame with List in R

开发者 https://www.devze.com 2023-02-25 19:35 出处:网络
Helo, I am trying to reshape a data.frame in R such that each row will repeat with a different value from a list, then the next row will repeat from a differing value from the second entry of the list

Helo, I am trying to reshape a data.frame in R such that each row will repeat with a different value from a list, then the next row will repeat from a differing value from the second entry of the list.

the list is called, wrk, dfx is the dataframe I want to reshape, and listOut is what I want to end up with. Thank you very much for your help.

> 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


How about replicate rows of dfx and cbind with unlisted wrk:

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


How about:

If wrk contains simple vectors like in your example:

> szs<-sapply(wrk, length)

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

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

If wrk contains dataframes:

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

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

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


How about:

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

Edit: Maybe you can eleborate a bit more, because this does seem to work:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号