我想根據資料框中匹配相同名稱的行數復制/重復三重嵌套串列。
這是一個三重嵌套串列:
list.A <- list(a = c(1,2,5,6), b = c(2,4,6,5), c = c(2,4,2,5))
list.B <- list(a = c(7,7,7,7), b = c(8,8,8,8), c = c(9,9,9,9))
weights <- list(list.A, list.B)
names(weights) <- c("list.A", "list.B")
list.A <- list(a = c(2,2,2,2), b = c(3,3,3,3), c = c(4,4,4,4))
list.B <- list(a = c(5,5,5,5), b = c(6,6,6,6), c = c(7,7,7,7))
scores <- list(list.A, list.B)
names(scores) <- c("list.A", "list.B")
megalist <- list(weights, scores)
names(megalist) <- c("weights", "scores")
megalist
> megalist
$weights
$weights$list.A
$weights$list.A$a
[1] 1 2 5 6
$weights$list.A$b
[1] 2 4 6 5
$weights$list.A$c
[1] 2 4 2 5
$weights$list.B
$weights$list.B$a
[1] 7 7 7 7
$weights$list.B$b
[1] 8 8 8 8
$weights$list.B$c
[1] 9 9 9 9
$scores
$scores$list.A
$scores$list.A$a
[1] 2 2 2 2
$scores$list.A$b
[1] 3 3 3 3
$scores$list.A$c
[1] 4 4 4 4
$scores$list.B
$scores$list.B$a
[1] 5 5 5 5
$scores$list.B$b
[1] 6 6 6 6
$scores$list.B$c
[1] 7 7 7 7
這是我想用來指示重復串列數量的資料框:
mydf <- as.data.frame(c("a", "a", "a", "b", "b", "c"))
colnames(mydf) <- "Freq"
> mydf
Freq
1 a
2 a
3 a
4 b
5 b
6 c
我希望這個輸出從megalist[["list.A"]]串列a中重復 3 次,因為它在 mydf 中有 3 行,b重復 2 次,因為它在 mydf 中有接下來的 2 行,并且c重復 1 次,因為它在 mydf 中有接下來的 1 行:
> megalist
$weights
$weights$list.A
$weights$list.A$a
[1] 1 2 5 6
$weights$list.A$a
[1] 1 2 5 6
$weights$list.A$a
[1] 1 2 5 6
$weights$list.A$b
[1] 2 4 6 5
$weights$list.A$b
[1] 2 4 6 5
$weights$list.A$c
[1] 2 4 2 5
$weights$list.B
$weights$list.B$a
[1] 7 7 7 7
$weights$list.B$b
[1] 8 8 8 8
$weights$list.B$c
[1] 9 9 9 9
$scores
$scores$list.A
$scores$list.A$a
[1] 2 2 2 2
$scores$list.A$a
[1] 2 2 2 2
$scores$list.A$a
[1] 2 2 2 2
$scores$list.A$b
[1] 3 3 3 3
$scores$list.A$b
[1] 3 3 3 3
$scores$list.A$c
[1] 4 4 4 4
$scores$list.B
$scores$list.B$a
[1] 5 5 5 5
$scores$list.B$b
[1] 6 6 6 6
$scores$list.B$c
[1] 7 7 7 7
我努力了:
megalist.repeated <- lapply(megalist, function(x, new) {
x[["list.A"]][new]
}, new = mydf$Freq)
但這擺脫了list.B,我想保持其他一切不變,只改變list.A。
我也試過:
megalist.repeated <- lapply(megalist, function(x, y)
if(x[["y"]]=="list.A")
lapply(y, function (y, new) {
y[new]
}, new = mydf$Freq
) else y)
但我的if宣告回傳長度為零。
uj5u.com熱心網友回復:
回圈嵌套listwith lapply,然后用 lambda 函式提取 ( [[) 'list.A' 元素,使用 themydf$Freq復制命名的內部串列list.A,將 ( <-) 賦值回同一個元素進行更新,并回傳整個物件x
megalist2 <- lapply(megalist, \(x) {
x[["list.A"]] <- x[["list.A"]][mydf$Freq]
x})
-檢查str舊物件與新物件的結構
> str(megalist2)
List of 2
$ weights:List of 2
..$ list.A:List of 6
.. ..$ a: num [1:4] 1 2 5 6
.. ..$ a: num [1:4] 1 2 5 6
.. ..$ a: num [1:4] 1 2 5 6
.. ..$ b: num [1:4] 2 4 6 5
.. ..$ b: num [1:4] 2 4 6 5
.. ..$ c: num [1:4] 2 4 2 5
..$ list.B:List of 3
.. ..$ a: num [1:4] 7 7 7 7
.. ..$ b: num [1:4] 8 8 8 8
.. ..$ c: num [1:4] 9 9 9 9
$ scores :List of 2
..$ list.A:List of 6
.. ..$ a: num [1:4] 2 2 2 2
.. ..$ a: num [1:4] 2 2 2 2
.. ..$ a: num [1:4] 2 2 2 2
.. ..$ b: num [1:4] 3 3 3 3
.. ..$ b: num [1:4] 3 3 3 3
.. ..$ c: num [1:4] 4 4 4 4
..$ list.B:List of 3
.. ..$ a: num [1:4] 5 5 5 5
.. ..$ b: num [1:4] 6 6 6 6
.. ..$ c: num [1:4] 7 7 7 7
> str(megalist)
List of 2
$ weights:List of 2
..$ list.A:List of 3
.. ..$ a: num [1:4] 1 2 5 6
.. ..$ b: num [1:4] 2 4 6 5
.. ..$ c: num [1:4] 2 4 2 5
..$ list.B:List of 3
.. ..$ a: num [1:4] 7 7 7 7
.. ..$ b: num [1:4] 8 8 8 8
.. ..$ c: num [1:4] 9 9 9 9
$ scores :List of 2
..$ list.A:List of 3
.. ..$ a: num [1:4] 2 2 2 2
.. ..$ b: num [1:4] 3 3 3 3
.. ..$ c: num [1:4] 4 4 4 4
..$ list.B:List of 3
.. ..$ a: num [1:4] 5 5 5 5
.. ..$ b: num [1:4] 6 6 6 6
.. ..$ c: num [1:4] 7 7 7 7
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/536376.html
標籤:r列表
