2016-04-04 4 views
-1

以下のリストとインデックス範囲の参照は、以下の関数の目的の出力を持っています。私の目的は、AとEで示されるインデックスリストの選択範囲に従ってメインリストのサブリストを選択してグループ化することです。これは2つの方法で試しましたが、これを実現できないようです。サブリストのサブリストの抽出と新しいリストへの指定された範囲のグループ化

RECX5 <- list (list (c(1 , 2 , 3 , 4 , 5) , c(6 , 7 , 8 , 9 , 10) , c(11 , 12 , 13 , 14, 15)) , list (c(-11 , -12 , -13 , -14 , -15) , c(16 , 17 , 18 , 19 , 20) , c(21 , 22 , 23 , 24, 25))) 

# SUB-SELECT LISTS 
A <- c(1,2) 

# SELECT RANGE FOR LISTS 
E <- data.frame (c(1,2) , c(1,5)) 

for (i in seq(nrow(E))) 
fun <- function (x) RECX5 [[ x ]] [ E[ ,1 ] [[ i ]] : E[ ,2 ] [[ i ]] ] 
D <- lapply ( seq(length (A)) , fun) 
# D 
[[1]] 
[[1]][[1]] 
[1] 6 7 8 9 10 

[[1]][[2]] 
[1] 11 12 13 14 15 

[[1]][[3]] 
NULL 

[[1]][[4]] 
NULL 


[[2]] 
[[2]][[1]] 
[1] 16 17 18 19 20 

[[2]][[2]] 
[1] 21 22 23 24 25 

[[2]][[3]] 
NULL 

[[2]][[4]] 
NULL 


DESIRED RESULT : 
[[1]] 
[[1]][[1]] 
1 2 3 4 5 
[[1]][[2]] 
-11 -12 -13 -14 -15 

[[2]] 
[[2]][[1]][[1]] 
6 7 8 9 10 
[[2][[1]][[2]] 
16 17 18 19 20 
[[2]][[2]][[1]] 
11 12 13 14 15 
[[2]][[2]][[2]] 
21 22 23 24 25 
+1

Appologiesが、それは、配列行方不明修正、感謝 – Barnaby

+0

は、私が "一般的に。" この質問を理解していませんまず、あなたの望む出力では、あなたは短縮形を使い、あなたの単一の括弧が二重括弧であることを意味していると思いますか?あなたの難しさの一部であるかもしれない 'R'の単一の' [''と '' [['は別の[意味](http://stackoverflow.com/a/1169495/241643)... – Philip

+0

第二に、上記の答えは「はい」です。希望の結果は、ネストされたリストを別のネストされたリストにわずかに再構成することです。しかし、文字通り、長さ5のベクトルの長さ3のリストの長さ2のリストを正確に求め、それを長さ2のリストに変換することを意味しますか?そのリストの最初の要素は長さ2のリストです長さ5のベクトルの長さ2のリストの長さ2のリストです。それとももっと一般的なことをしたいのですか? – Philip

答えて

-1

これは意図した構造を生成しますが、明らかにジェネリックではありません:あなたは、より一般的に

> list(list(RECX5[[1]][[1]],RECX5[[2]][[1]]), 
+  list(list(RECX5[[1]][[2]],RECX5[[2]][[2]]), 
+   list(RECX5[[1]][[3]],RECX5[[2]][[3]]))) 
[[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 

何をしたいですか?一般的には[申し訳ありませんが]、インデックスレベルを選択してlapplyインデックスの一部を入力して上記を一般化することができますfunction(x) [that expression]ここで、機能化するインデックスレベルでの複数コールはxに置き換えられます。私のいくつかのコメントパー

EDIT/UPDATE

私はあなたが一般的にあなたのソリューションへの入力を指定しているが、私はどのような要件を示すべきであることをここアプローチをスペルアウトしたと思うか理解していません解決のためのものです。あなたは実際に私が。

ノートにハードコーディングされていないことを保持するいくつかの仮定を持っていない場合は理想的には、あなたがこの戦略を簡素化することができます、はい、この解決策はまだ任意の深さの結果リストを処理する際の一般的なではありません。それは実行可能ですが、さらに大きな混乱です。

まず、構造をテーブルとして構築するためのリストを歩みましょう。これは、簡単に最終的な関数呼び出しでリストを横断するようになります:今

library(data.table) 
build.structure <- function(src) { 
    rbindlist(lapply(1:length(src), 
       function(x) data.table(input.position.depth.0=rep(x,length(src[[x]])), 
            input.position.depth.1=1:length(src[[x]])))) 
} 

struct <- build.structure(RECX5) 
> struct 
    input.position.depth.0 input.position.depth.1 
1:      1      1 
2:      1      2 
3:      1      3 
4:      2      1 
5:      2      2 
6:      2      3 

手動あなたが関数に渡します望ましい結果の構造を構築します。これは私があなたがやろうとしたことを理解していないところです。何らかの形で結果関数にはすべてという情報が必要です。入力の各要素は、出力のリスト構造に正確に置かれますか?私は、この例では、これは耐え難いほど特定作られましたが、あなたは仮定を単純化している場合は、このディテールの一部を省略することができるでしょう:列にダウンこのテキストを読む

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 
+0

これは、リスト内のサブリストの場所を変更する、質問内のリストの索引付きリストです。私は、元の注文リストから、メインリストの所在を示すインデックス範囲ごとに関心のサブリストを抽出し、そのリストをインデックスごとにリストする機能を目指します。 – Barnaby

+0

しかし、あなたの入力に希望する出力リストの構造をどのようにエンコードするのかは指定していません。私はあなたに '2のリストが届くのを見ていない。 $:リスト2 .. $:num [1:5] 1 2 3 4 5 .. $:num [1:5] -11 -12 $:num [1:5] 6 7 8 9 10 .. $:num [1:5]リストの2 。$:num [1:5] 6 7 8 9 10 .. $:num [1:5 ] 16 17 18 19 20 .. $:リスト2 .. .. $ num [1:5] 11 12 13 14 15 .. .. $ num [1:5] 21 22 23 24 25あなたの 'A'と' E'から。これが私があなたに何が一般的であるかを尋ねている理由です。結果は*常に*そのようなリストですか、ちょうど異なるソースですか?あるいは可変構造の結果ですか? – Philip

+0

特に、入力オブジェクト 'E'に数字2と5がありますが、例として与えられているケースの「正解」には数字5が表示されません。ですから、どういうわけか、あからさまな前提があるはずです。あなたのサンプル入力を取るジェネリック関数は、答えの出力を生成しなければなりません。これは、何とか私の答えの中の5番のマジックナンバー(インデックス、すべて1,2または3)を計算することを知る必要があることを意味します。これは理にかなっていますか? – Philip

-1
Use : lapply (list name , '[' , list number) 


RECX5 <- list (list (c(1 , 2 , 3 , 4 , 5) , c(6 , 7 , 8 , 9 , 10) , c(11 , 12 , 13 , 14, 15)) , list (c(-11 , -12 , -13 , -14 , -15) , c(16 , 17 , 18 , 19 , 20) , c(21 , 22 , 23 , 24, 25))) 
# SUBSELECT LISTS. 
A  <- c (1 , 2) 
# SELECT LISTS. 
B  <- data.frame (c (1 , 2) , c(1 , 5)) 

# Possible solution, however I does the null results of running the sequence over the function so it adds two null list to at the end of the last lists. 

fun  <- function (x) (B[,x] [2] - B[,x] [1]) 
E1  <- lapply (seq (nrow (B)) , fun) 
fun  <- function (x) (E[,2] [[x]] - E[,1] [[x]])+1 
E1  <- lapply (seq (nrow (E)) , fun) 
fun  <- function (x) seq (E1[[ x ]]) 
E2  <- lapply (seq (length (E1)) , fun) 
E3  <- relist (seq (unlist (E2)) , E2) 
fun <- function (y) lapply (RECX5 , '[' , unlist (E3[ y ])) 
E4  <- lapply (seq(length(E3)) , fun) 
+0

の理由投票を行います – Barnaby

関連する問題