Home > front end >  mapping vector based on a list
mapping vector based on a list

Time:10-07

Assume we have the following vector :

v1=c(8, 18, 19, 22, 23, 26, 36, 51, 52, 69, 72, 78, 89, 94, 105)

and its associated list :

L1=list(c(8, 18, 19),c(22, 23, 26, 36),c(51, 52),c(69, 72, 78),c(89, 94),c(105))

i need to convert v1 to its associated L1 level :

expected_output=c(1, 1, 1, 2, 2, 2,2, 3, 3, 4, 4, 4, 5, 5, 6)

Note that v1 is not necessarily having the same number of L1 entries.

For example :

v2=c(22,22,26,105)

should be mapped to c(2,2,2,6)

Is there any trivial function to do that , thanks

CodePudding user response:

I would convert your list to a look-up table and then use match:

d1 = data.frame(v = unlist(L1), i = rep(seq_along(L1), lengths(L1)))
d1$i[match(v1, d1$v)]
# [1] 1 1 1 2 2 2 2 3 3 4 4 4 5 5 6

v2 = c(22,22,26,105)
d1$i[match(v2, d1$v)]
# [1] 2 2 2 6

You could do it with a double sapply instead (as below), but the above seems cleaner to me.

sapply(v1, \(v) which(sapply(L1, \(l) v %in% l)))
# [1] 1 1 1 2 2 2 2 3 3 4 4 4 5 5 6

CodePudding user response:

We could use stack on a named list and extract the second column

stack(setNames(L1, seq_along(L1)))[,2]
[1] 1 1 1 2 2 2 2 3 3 4 4 4 5 5 6
  •  Tags:  
  • r
  • Related