r - Selecting multiple elements from list of matrix that match elements of list of vector -


i have 2 variables, 1 list of matrices , other list of vectors.

people: load("https://dl.dropboxusercontent.com/u/22681355/a.rdata")

mat: load("https://dl.dropboxusercontent.com/u/22681355/b.rdata")

i go [[1]] [[99]] along elements in people , select rows in mat first column of mat matches people , return second column of mat.

i tried:

lapply(seq_along(people), function(i) mat[mat[,1,i] ==      people[i], 2, i])  

however cannot handle fact there 1 matching entry while in other cases there can 2 or 3 matching entries.

can modifying code?

small example:

people:

[[1]] [1] 34 56 7 [[2]] [1] 13 93 [[3]] [1] 42 

mat

,,1     [,1] [,2] [,3] [1,] 34   **2**    1 [2,] 56   **2**    1 [3,] 7    **2**    2  ,,2     [,1] [,2] [,3] [1,] 9    2    1 [2,] 13   **2**    1 [3,] 71   2    2  ,,3     [,1] [,2] [,3] [1,] 90   2    1 [2,] 1    2    1 [3,] 42   **2**    2 

the output be:

i'm not sure if pulling out want. comment matching elements of people[1] mat[,,1], people[2] mat[,,2] , on , returning value in second column of mat if there id match.

i have used first 3 values people , mat rdata.

# reduce problem # mat <- mat[,,1:3] #people <- people[1:3]  # data mat <- structure(c(82, 59, 6, 1, 1, 2, 1, 1, 2, 17, 51, 18, 1, 2, 1,                 1, 2, 1, 31, 26, 40, 1, 1, 1, 1, 1, 1), .dim = c(3l, 3l, 3l))  people <- list(6, 51, c(31, 26, 40))   l <- lapply(seq(people), function(x) {             lapply(people[x] , function(z) {                    mat[,,x][,2][match(z, mat[,,x][,1])]        })})   #--------------------------------------------------------------------  # output r> mat , , 1       [,1] [,2] [,3] [1,]   82    1    1 [2,]   59    1    1 [3,]    6    2    2  , , 2       [,1] [,2] [,3] [1,]   17    1    1 [2,]   51    2    2 [3,]   18    1    1  , , 3       [,1] [,2] [,3] [1,]   31    1    1 [2,]   26    1    1 [3,]   40    1    1  r> people [[1]] [1] 6  [[2]] [1] 51  [[3]] [1] 31 26 40  r> l [[1]] [[1]][[1]] [1] 2   [[2]] [[2]][[1]] [1] 2   [[3]] [[3]][[1]] [1] 1 1 1 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -