Manipulating a List and a List of Lists of Data Frames

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))

I have two lists L (List of vectors) and InputList (List of Lists of Data Frames). I have to search the position of vector (1,1) in list L and the return the corresponding list of dataframe from InputList

for instance in the above example vector (1,1) is the third element of the Inputlist so I have to return the third list of dataframes from L is List (B,C,A)

Answers


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

I'm assuming you have a way of defining your test vector. In your example you used c(1, 1).

test_vector <- c(1, 1)

Define a function that does what you want. If the value in InputList is the same as your test_vector, return the element of L that matches the index x.

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

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

However this returns NULL where there isn't match. The step below removes the NULLs in the list.

> 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

Need Your Help

Accessing Sqlite Database from Sd Card

android database sqlite sd-card

I have stored my database on the SD card and now I'm trying to access it but it gives me an error :