私はあなたが一般的にあなたのソリューションへの入力を指定しているが、私はどのような要件を示すべきであることをここアプローチをスペルアウトしたと思うか理解していません解決のためのものです。あなたは実際に私が。
struct[,result.position.depth.0:=c(1, 2, 2, 1, 2, 2)]
struct[,result.position.depth.1:=c(1, 1, 2, 2, 1, 2)]
struct[,result.position.depth.2:=c(NA, 1, 1, NA, 2, 2)]
注は、得られています要素のリストの位置。あなたは全体の読み表を表示する場合:
> struct
input.position.depth.0 input.position.depth.1 result.position.depth.0 result.position.depth.1
1: 1 1 1 1
2: 2 1 1 2
3: 1 2 2 1
4: 2 2 2 1
5: 1 3 2 2
6: 2 3 2 2
result.position.depth.2
1: NA
2: NA
3: 1
4: 2
5: 1
6: 2
を、私は以下の書き込み機能は、新しいリストを前提とし、奥行きインデックスを経由して要素の辞書順に建てられ、それに応じこのテーブルをソートします(
setkey(struct,result.position.depth.0,
result.position.depth.1,
result.position.depth.2)
をここでは一般性についての説明がありますが、これは2つのレベルよりも深くなる必要がある場合は、データをmelt
に追加し、深度欄を追加して深さをその列の値として参照し、それに応じて深さを切り出して深さを出します意図された深度をハードコーディングするのではなく、我々だけ内蔵テーブルの行で指定された)
次に便利な機能は、入力されたリストからデータを抽出する:今
extract <- function(src,struct,a.row) {
src[[struct[a.row,input.position.depth.0]]][[struct[a.row,input.position.depth.1]]]
}
長い部分。
まずグローバルオブジェクトを作成します。 (これは単に私たちが参照を心配する必要はありませんを意味します。これを回避することができますが、私は気にするつもりはありません。)
result <<- list()
は、その後のは、[インラインコメントを参照してください]このオブジェクトを変更する機能を使ってみましょう
get.lists.from.structure <- function(src, struct) {
lapply(1:nrow(struct), function(the.row) {
#Extract the value that we'll insert next
val <- extract(src,struct,the.row)
#If there's not already a slot at the top level for this value,
#make an empty list there so we'll have a place to put it.
#Without this you'll get subscript out of bounds errors
if(length(result) < struct[the.row,result.position.depth.0]) {
result[[struct[the.row,result.position.depth.0]]] <<- list()
}
#Now if the resulting object is getting stuck one level down,
#take the first branch; just put it there.
if(struct[the.row,is.na(result.position.depth.2)]) {
result[[struct[the.row,result.position.depth.0
]]][[struct[the.row,result.position.depth.1]]] <<- val
} else {
#otherwise, take this branch, but WAIT! Repeat the same check;
#if there's not already a slot at this depth, put
#an empty list in
if(length(result[[struct[the.row,result.position.depth.0]]]) <
struct[the.row,result.position.depth.1]) {
result[[struct[the.row,result.position.depth.0
]]][[struct[the.row,result.position.depth.1]]] <<- list()
}
#then put the value in at its intended position.
result[[struct[the.row,result.position.depth.0
]]][[struct[the.row,result.position.depth.1
]]][[struct[the.row,result.position.depth.2]]] <<- val
}
})
}
OK、今、私たちは、この(それは印刷し何を無視する、またはinvisible
を追加)
get.lists.from.structure(RECX5,struct)
を実行し、それがに正しい副作用を持っていたことを確認することができます:
> result
[[1]]
[[1]][[1]]
[1] 1 2 3 4 5
[[1]][[2]]
[1] -11 -12 -13 -14 -15
[[2]]
[[2]][[1]]
[[2]][[1]][[1]]
[1] 6 7 8 9 10
[[2]][[1]][[2]]
[1] 16 17 18 19 20
[[2]][[2]]
[[2]][[2]][[1]]
[1] 11 12 13 14 15
[[2]][[2]][[2]]
[1] 21 22 23 24 25
Appologiesが、それは、配列行方不明修正、感謝 – Barnaby
は、私が "一般的に。" この質問を理解していませんまず、あなたの望む出力では、あなたは短縮形を使い、あなたの単一の括弧が二重括弧であることを意味していると思いますか?あなたの難しさの一部であるかもしれない 'R'の単一の' [''と '' [['は別の[意味](http://stackoverflow.com/a/1169495/241643)... – Philip
第二に、上記の答えは「はい」です。希望の結果は、ネストされたリストを別のネストされたリストにわずかに再構成することです。しかし、文字通り、長さ5のベクトルの長さ3のリストの長さ2のリストを正確に求め、それを長さ2のリストに変換することを意味しますか?そのリストの最初の要素は長さ2のリストです長さ5のベクトルの長さ2のリストの長さ2のリストです。それとももっと一般的なことをしたいのですか? – Philip