2016-08-16 221 views
1

這是來自此one的後續問題,我想在列表中創建一個列表,現在我希望能夠訪問嵌套列表並在其中實現函數太。如何使用循環訪問列表中的所有列表

我有3個列表,其內具有兩個列表:

my_list 

    structure(list(a = structure(list(a = structure(list(value = c(1L, 
4L), class = structure(c(1L, 1L), .Label = c("a", "b", "c"), class = "factor")), .Names = c("value", 
"class"), row.names = c(1L, 4L), class = "data.frame"), b = structure(list(
    value = c(2, 1), class = structure(c(2L, 2L), .Label = c("a", 
    "b", "c"), class = "factor")), .Names = c("value", "class" 
), row.names = c(2L, 5L), class = "data.frame")), .Names = c("a", 
"b")), b = structure(list(a = structure(list(value = c(6, 1), 
    class = structure(c(1L, 1L), .Label = c("a", "b", "c", "d" 
    ), class = "factor")), .Names = c("value", "class"), row.names = c(1L, 
5L), class = "data.frame"), b = structure(list(value = c(5, 1 
), class = structure(c(2L, 2L), .Label = c("a", "b", "c", "d" 
), class = "factor")), .Names = c("value", "class"), row.names = c(2L, 
6L), class = "data.frame")), .Names = c("a", "b")), c = structure(list(
    a = structure(list(value = 5:6, class = structure(c(1L, 1L 
    ), .Label = c("a", "b", "c", "d", "e"), class = "factor")), .Names = c("value", 
    "class"), row.names = 5:6, class = "data.frame"), b = structure(list(
     value = c(4, 1), class = structure(c(2L, 2L), .Label = c("a", 
     "b", "c", "d", "e"), class = "factor")), .Names = c("value", 
    "class"), row.names = c(4L, 7L), class = "data.frame")), .Names = c("a", 
"b"))), .Names = c("a", "b", "c")) 

    $a 
    $a$a 
     value class 
     1  a 
     4  a 

    $a$b 
     value class 
     2  b 
     1  b 


    $b 
    $b$a 
     value class 
     6  a 
     1  a 

    $b$b 
     value class 
     5  b 
     1  b 


    $c 
    $c$a 
     value class 
     5  a 
     6  a 

    $c$b 
     value class 
     4  b 
     1  b 

我通過value != 1subset每個嵌套列表使用循環使得所得到的名單看起來是這樣的:

my_list 
$a 
     $a$a 
      value class 
      4  a 

     $a$b 
      value class 
      2  b 



     $b 
     $b$a 
      value class 
      6  a 


     $b$b 
      value class 
      5  b 



     $c 
     $c$a 
      value class 
      5  a 
      6  a 

     $c$b 
      value class 
      4  b 

有了下面的代碼,我只能訪問list [[1]][[1]]list[[2]][[2]]list[[3]][[3]]。我如何通過循環訪問其他人並應用subset函數?

for (i in 1:length(my_list)) { 
    my_list[[i]][[i]] <- subset(my_list[[i]][[i]],my_list[[i]][[i]]$value=1) 
} 
+0

我注意到您沒有鏈接到您昨天就此主題提出的問題。如果兩者不同,請說明http://stackoverflow.com/questions/38954154/using-a-loop-or-vectorisation-to-subset-a-list-by-multiple-elements-in-a-vecto –

+0

好主意,編輯好吧 – Bonono

+1

好點 - 現在修好了 – Bonono

回答

1

我們可以使用嵌套lapplyrapply

lapply(my_list, function(x) lapply(x, function(y) subset(y, value!=1))) 
#$a 
#$a$a 
# value class 
#4  4  a 

#$a$b 
# value class 
#2  2  b 


#$b 
#$b$a 
# value class 
#1  6  a 

#$b$b 
# value class 
#2  5  b 


#$c 
#$c$a 
# value class 
#5  5  a 
#6  6  a 

#$c$b 
# value class 
#4  4  b 
+0

我真的應該學習如何使用lapply。是不是可以用循環做到這一點? – Bonono

+0

@Bonono你有一個嵌套循環,一種方法是通過循環或者將其轉換爲單個數據集來進行嵌套循環,過濾'值'然後再轉換回嵌套表單(這是不必要的複雜) – akrun

+1

我同意,當你這樣說時,這聽起來很荒謬。謝謝你的幫助akrun,你一直都很棒。我一定會在博士學位上承認你! – Bonono

1

對於那些想知道如何爲這個問題做一個嵌套的循環 - 我必須幫助摸不着頭腦:

for (i in 1:length(my_list)) { 
    for (j in 1:2) { 
    my_list[[i]][[j]] <- subset(my_list[[i]][[j]],my_list[[i]][[j]]$value!=1) 
    } 
} 
+1

在'subset'裏面,你可以使用'value!= 1',即'my_list [[i]] [[j]] - 子集(my_list [[i]] [[j]],值!= 1 )' – akrun