How to turn a list inside out?

Viewed 516

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.

7 Answers

Here is a base solution ...

list <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))

rep(1:length(list), sapply(list, length))[order(unlist(list))]

Can I suggest an old-fashioned loop:

# your list
x <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
# the data in the list as vector
num <- unlist( x )
# the variable that will be the position vector
pos <- NULL

# loop through the possible position, see which number it contains
# find which "group it belongs to, and add that finding to the position vector
for( i in 1:length( num ) )
    for( j in  1:length( x ) )
        if( i %in% x[[j]] ) pos <- c( pos, j )

pos
[1] 3 2 1 1 1 2 3 1 2 2

Check this solution:

library(tidyverse)
library(magrittr)
library(wrapr)

list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7)) %.>%
  tibble(x = .) %>%
  mutate(rn = row_number()) %>%
  unnest() %>%
  arrange(x) %$%
  set_names(rn, x) %>%
  as.list()
x <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))

Following 3 forms will get the same outputs:

library(tidyverse)

# (1)
x %>% set_names(1:3) %>% stack %>% arrange(values) %>% select(ind)

# (2)
x %>% enframe %>% unnest %>% arrange(value) %>% select(name)

# (3)
x %>% (reshape2::melt) %>% arrange(value) %>% select(L1)

Also in base, something like this

L <- as.list(setNames( rep(1:length(lengths(l)), lengths(l)), unlist(l)))
# if wanted, sort it with
L[as.character(sort(as.integer(names(L))))]
# if wanted, unname with
unname(L)

with l <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7)).

Or wrapped in a function

list_inside_out <- function (l, unName = TRUE) { 
  l2 <- lengths(l)
  out <- as.list(setNames(rep(1:length(l2), l2), unlist(l)))
  out <- out[as.character(sort(as.integer(names(out))))] 
  if (unName) return(unname(out))
  out
}
list_inside_out(l)
# [[1]]
# [1] 3
# 
# [[2]]
# [1] 2
# 
# [[3]]
# [1] 1
# ...

A solution using purrr. dat2 is the final output, an integer vector.

dat <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))

library(purrr)

dat2 <- dat %>%
  imap(~set_names(.x, rep(.y, length(.x)))) %>%
  unlist() %>%
  sort() %>%
  names() %>%
  as.integer()
dat2
# [1] 3 2 1 1 1 2 3 1 2 2

Using tidyverse and purr::imap_dfr we can create a tibble with the values and indices side by side, arrange by value and pull the indices :

list_ <- list(c(3, 4, 5, 8), c(2, 6, 9, 10), c(1, 7))
library(tidyverse)
imap_dfr(list_,~tibble(.x,.y)) %>% arrange(.x) %>% pull(.y) %>% as.list

# [[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

Less pretty translated in base R (same output) :

with(
  as.data.frame(do.call(rbind,Map(cbind,a = list_, b =seq_along(list_)))),
  as.list(b[order(a)]))
Related