First you can turn your list into data.frame, which maps the values ββto their corresponding index in the list:
ll <- list(c("7", "12", "26", "29"), c("11", "36"), c("20", "49"), c("39", "41")) df <- data.frame(value = unlist(ll), index = rep(seq_along(ll), lapply(ll, length))) df
Then write a function using match
to find the index of the first occurrence of the given value:
find.idx <- function(val)df$index[match(val, df$value)]
You can call this function one value or many times since match
vectorized:
find.idx("36") # [1] 2 find.idx(c("36", "41", "99")) # [1] 2 4 NA
Of course, you can also run it through lapply
, especially if you plan to run it in parallel:
lapply(c("36", "41", "99"), find.idx) # [[1]] # [1] 2 # # [[2]] # [1] 4 # # [[3]] # [1] NA
There are many options to run this last bit in parallel. I would recommend you weigh your options by searching http://cran.r-project.org/web/views/HighPerformanceComputing.html .