I've got a following list:
> list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
[[1]]
[1] 3 4 5 8
[[2]]
[1] 2 6 9 10
[[3]]
[1] 1 7
So we can say that 3 belongs to group 1, 6 belongs to group 2, 7 belongs to group 3 and so on. I need a reverse mapping, i.e. to every number I want to have a group id that it belongs to (see expected output):
> list(3, 2, 1, 1, 1, 2, 3, 1, 2, 2)
[[1]]
[1] 3
[[2]]
[1] 2
[[3]]
[1] 1
[[4]]
[1] 1
[[5]]
[1] 1
[[6]]
[1] 2
[[7]]
[1] 3
[[8]]
[1] 1
[[9]]
[1] 2
[[10]]
[1] 2
I thought purrr::transpose
should do the job but it doesn't exactly do what I intend, is it? How can it be done?
PS. Ultimately, I need just a vector of a form: 3 2 1 1 1 2 3 1 2 2
, but having above I think unlist()
is enough to convert.