2012-06-14 79 views
0
A = data.frame(a = c(1:10), b = c(11:20)) 
B = data.frame(a = c(101:110), b = c(111:120)) 
C = data.frame(a = c(5:8), b = c(55:58)) 

L = list(list(B), list(A,C), list(B,C,A), list(B,C)) 

InputList = InputList = list(c(0.9,0.8),c(0.98,0.5),c(1, 1),c(1.5,1.2)) 

我有兩個列表L(列表矢量)和InputList(列表數據框列表)。我必須搜索列表L中向量(1,1)的位置,並返回InputList中對應的數據幀列表。例如,在上面的示例中,向量(1,1)是輸入列表的第三個元素,因此InputList(012)我不得不返回第三個列表中的數據幀列表(B,C,A)操作數據框列表和列表數據框列表

回答

0
test<-mapply(function(x,y){paste(x,collapse="~")==y},InputList,paste(c(1,1),collapse="~")) 
L[test] 
2

我假設你有一種方法來定義你的測試向量。在你的例子中,你使用c(1, 1)

test_vector <- c(1, 1) 

定義一個你想要的功能。如果InputList中的值與您的test_vector相同,則返回與索引x匹配的元素L

myfun <- function(x, test_vec=test_vector) { 
    if(identical(InputList[[x]], test_vec)) { 
    return(L[[x]]) 
    } 
} 

out <- lapply(seq_along(InputList), myfun) 

但是,這返回NULL哪裏不匹配。以下步驟將刪除列表中的NULL

> out[!sapply(out, is.null)] 
[[1]] 
[[1]][[1]] 
    a b 
1 101 111 
2 102 112 
3 103 113 
4 104 114 
5 105 115 
6 106 116 
7 107 117 
8 108 118 
9 109 119 
10 110 120 

[[1]][[2]] 
    a b 
1 5 55 
2 6 56 
3 7 57 
4 8 58 

[[1]][[3]] 
    a b 
1 1 11 
2 2 12 
3 3 13 
4 4 14 
5 5 15 
6 6 16 
7 7 17 
8 8 18 
9 9 19 
10 10 20 
+0

我早該換到'all.equal'。你可以用「相同」和浮動來解決問題。 – Justin