You could first turn your list into a data.frame that maps 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
# value index
# 1 7 1
# 2 12 1
# 3 26 1
# 4 29 1
# 5 11 2
# 6 36 2
# 7 20 3
# 8 49 3
# 9 39 4
# 10 41 4
Then, write a function using match
for finding the index of the first occurrence of a given value:
find.idx <- function(val)df$index[match(val, df$value)]
You can call this function on a single value, or many at a time since match
is 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
For running this last bit in parallel, there are many, many options. I would recommend you weigh your options by searching through http://cran.r-project.org/web/views/HighPerformanceComputing.html.