How to turn a list inside out?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







8















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.










share|improve this question























  • My apologies but I can't make out how to get 3 2 1 1 1 2 3 1 2 2 from given input, Its atleast not clear to me. Thanks

    – PKumar
    Jan 13 at 7:03











  • @PKumar: 1 is in group 3, 2 is in group 2, 3 is in group 1, 4 is in group 1, 5 is in group 1 ...

    – Khaynes
    Jan 13 at 7:08






  • 1





    We start from 1 and see which group 1 belongs to - we can see it's in 3rd group, namely 3rd element of a list. Then we go with 2:10 and check the same.

    – jakes
    Jan 13 at 7:08


















8















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.










share|improve this question























  • My apologies but I can't make out how to get 3 2 1 1 1 2 3 1 2 2 from given input, Its atleast not clear to me. Thanks

    – PKumar
    Jan 13 at 7:03











  • @PKumar: 1 is in group 3, 2 is in group 2, 3 is in group 1, 4 is in group 1, 5 is in group 1 ...

    – Khaynes
    Jan 13 at 7:08






  • 1





    We start from 1 and see which group 1 belongs to - we can see it's in 3rd group, namely 3rd element of a list. Then we go with 2:10 and check the same.

    – jakes
    Jan 13 at 7:08














8












8








8








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.










share|improve this question














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.







r base purrr






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 13 at 6:51









jakesjakes

476315




476315













  • My apologies but I can't make out how to get 3 2 1 1 1 2 3 1 2 2 from given input, Its atleast not clear to me. Thanks

    – PKumar
    Jan 13 at 7:03











  • @PKumar: 1 is in group 3, 2 is in group 2, 3 is in group 1, 4 is in group 1, 5 is in group 1 ...

    – Khaynes
    Jan 13 at 7:08






  • 1





    We start from 1 and see which group 1 belongs to - we can see it's in 3rd group, namely 3rd element of a list. Then we go with 2:10 and check the same.

    – jakes
    Jan 13 at 7:08



















  • My apologies but I can't make out how to get 3 2 1 1 1 2 3 1 2 2 from given input, Its atleast not clear to me. Thanks

    – PKumar
    Jan 13 at 7:03











  • @PKumar: 1 is in group 3, 2 is in group 2, 3 is in group 1, 4 is in group 1, 5 is in group 1 ...

    – Khaynes
    Jan 13 at 7:08






  • 1





    We start from 1 and see which group 1 belongs to - we can see it's in 3rd group, namely 3rd element of a list. Then we go with 2:10 and check the same.

    – jakes
    Jan 13 at 7:08

















My apologies but I can't make out how to get 3 2 1 1 1 2 3 1 2 2 from given input, Its atleast not clear to me. Thanks

– PKumar
Jan 13 at 7:03





My apologies but I can't make out how to get 3 2 1 1 1 2 3 1 2 2 from given input, Its atleast not clear to me. Thanks

– PKumar
Jan 13 at 7:03













@PKumar: 1 is in group 3, 2 is in group 2, 3 is in group 1, 4 is in group 1, 5 is in group 1 ...

– Khaynes
Jan 13 at 7:08





@PKumar: 1 is in group 3, 2 is in group 2, 3 is in group 1, 4 is in group 1, 5 is in group 1 ...

– Khaynes
Jan 13 at 7:08




1




1





We start from 1 and see which group 1 belongs to - we can see it's in 3rd group, namely 3rd element of a list. Then we go with 2:10 and check the same.

– jakes
Jan 13 at 7:08





We start from 1 and see which group 1 belongs to - we can see it's in 3rd group, namely 3rd element of a list. Then we go with 2:10 and check the same.

– jakes
Jan 13 at 7:08












7 Answers
7






active

oldest

votes


















3














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))]





share|improve this answer





















  • 3





    Nice solution, you could fully vectorize this using rep(seq_along(l), lengths(l))[order(unlist(l))] (not sure what you are using as.numeric for).

    – David Arenburg
    Jan 13 at 8:25








  • 2





    Regarding "doesn't exactly answer my main question with list converting inside-out" I think you need to clarify your question. The way you describe and present your desired result it seems like you simply want to get the list index for each value (whih this and other answers achieve). I fail to see the 'inside-out' part ;)

    – Henrik
    Jan 13 at 10:11













  • @DavidArenburg Good point (original solution was a bit longer, so this was just noise left over ;-))

    – Khaynes
    Jan 13 at 22:03



















2














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





share|improve this answer































    2














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





    share|improve this answer

































      1














      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()





      share|improve this answer
























      • Is the 5th line a mistake %.>%?

        – Khaynes
        Jan 13 at 8:00











      • No. It's a pipe from wrapr package.

        – Paweł Chabros
        Jan 13 at 8:11






      • 1





        Why do you use %.>% in wrapr additionally instead of using %>% in tidyverse? They will get the same outcome in this case.

        – Darren Tsai
        Jan 13 at 8:19











      • You are right. My bad.

        – Paweł Chabros
        Jan 13 at 8:58



















      1














      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)





      share|improve this answer

































        0














        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





        share|improve this answer































          0














          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)]))





          share|improve this answer


























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54166704%2fhow-to-turn-a-list-inside-out%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            7 Answers
            7






            active

            oldest

            votes








            7 Answers
            7






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            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))]





            share|improve this answer





















            • 3





              Nice solution, you could fully vectorize this using rep(seq_along(l), lengths(l))[order(unlist(l))] (not sure what you are using as.numeric for).

              – David Arenburg
              Jan 13 at 8:25








            • 2





              Regarding "doesn't exactly answer my main question with list converting inside-out" I think you need to clarify your question. The way you describe and present your desired result it seems like you simply want to get the list index for each value (whih this and other answers achieve). I fail to see the 'inside-out' part ;)

              – Henrik
              Jan 13 at 10:11













            • @DavidArenburg Good point (original solution was a bit longer, so this was just noise left over ;-))

              – Khaynes
              Jan 13 at 22:03
















            3














            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))]





            share|improve this answer





















            • 3





              Nice solution, you could fully vectorize this using rep(seq_along(l), lengths(l))[order(unlist(l))] (not sure what you are using as.numeric for).

              – David Arenburg
              Jan 13 at 8:25








            • 2





              Regarding "doesn't exactly answer my main question with list converting inside-out" I think you need to clarify your question. The way you describe and present your desired result it seems like you simply want to get the list index for each value (whih this and other answers achieve). I fail to see the 'inside-out' part ;)

              – Henrik
              Jan 13 at 10:11













            • @DavidArenburg Good point (original solution was a bit longer, so this was just noise left over ;-))

              – Khaynes
              Jan 13 at 22:03














            3












            3








            3







            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))]





            share|improve this answer















            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))]






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 13 at 22:02

























            answered Jan 13 at 7:12









            KhaynesKhaynes

            727721




            727721








            • 3





              Nice solution, you could fully vectorize this using rep(seq_along(l), lengths(l))[order(unlist(l))] (not sure what you are using as.numeric for).

              – David Arenburg
              Jan 13 at 8:25








            • 2





              Regarding "doesn't exactly answer my main question with list converting inside-out" I think you need to clarify your question. The way you describe and present your desired result it seems like you simply want to get the list index for each value (whih this and other answers achieve). I fail to see the 'inside-out' part ;)

              – Henrik
              Jan 13 at 10:11













            • @DavidArenburg Good point (original solution was a bit longer, so this was just noise left over ;-))

              – Khaynes
              Jan 13 at 22:03














            • 3





              Nice solution, you could fully vectorize this using rep(seq_along(l), lengths(l))[order(unlist(l))] (not sure what you are using as.numeric for).

              – David Arenburg
              Jan 13 at 8:25








            • 2





              Regarding "doesn't exactly answer my main question with list converting inside-out" I think you need to clarify your question. The way you describe and present your desired result it seems like you simply want to get the list index for each value (whih this and other answers achieve). I fail to see the 'inside-out' part ;)

              – Henrik
              Jan 13 at 10:11













            • @DavidArenburg Good point (original solution was a bit longer, so this was just noise left over ;-))

              – Khaynes
              Jan 13 at 22:03








            3




            3





            Nice solution, you could fully vectorize this using rep(seq_along(l), lengths(l))[order(unlist(l))] (not sure what you are using as.numeric for).

            – David Arenburg
            Jan 13 at 8:25







            Nice solution, you could fully vectorize this using rep(seq_along(l), lengths(l))[order(unlist(l))] (not sure what you are using as.numeric for).

            – David Arenburg
            Jan 13 at 8:25






            2




            2





            Regarding "doesn't exactly answer my main question with list converting inside-out" I think you need to clarify your question. The way you describe and present your desired result it seems like you simply want to get the list index for each value (whih this and other answers achieve). I fail to see the 'inside-out' part ;)

            – Henrik
            Jan 13 at 10:11







            Regarding "doesn't exactly answer my main question with list converting inside-out" I think you need to clarify your question. The way you describe and present your desired result it seems like you simply want to get the list index for each value (whih this and other answers achieve). I fail to see the 'inside-out' part ;)

            – Henrik
            Jan 13 at 10:11















            @DavidArenburg Good point (original solution was a bit longer, so this was just noise left over ;-))

            – Khaynes
            Jan 13 at 22:03





            @DavidArenburg Good point (original solution was a bit longer, so this was just noise left over ;-))

            – Khaynes
            Jan 13 at 22:03













            2














            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





            share|improve this answer




























              2














              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





              share|improve this answer


























                2












                2








                2







                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





                share|improve this answer













                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






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 13 at 8:53









                vaettchenvaettchen

                5,5251332




                5,5251332























                    2














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





                    share|improve this answer






























                      2














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





                      share|improve this answer




























                        2












                        2








                        2







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





                        share|improve this answer















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






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Jan 13 at 9:50

























                        answered Jan 13 at 8:59









                        nikoniko

                        3,3291421




                        3,3291421























                            1














                            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()





                            share|improve this answer
























                            • Is the 5th line a mistake %.>%?

                              – Khaynes
                              Jan 13 at 8:00











                            • No. It's a pipe from wrapr package.

                              – Paweł Chabros
                              Jan 13 at 8:11






                            • 1





                              Why do you use %.>% in wrapr additionally instead of using %>% in tidyverse? They will get the same outcome in this case.

                              – Darren Tsai
                              Jan 13 at 8:19











                            • You are right. My bad.

                              – Paweł Chabros
                              Jan 13 at 8:58
















                            1














                            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()





                            share|improve this answer
























                            • Is the 5th line a mistake %.>%?

                              – Khaynes
                              Jan 13 at 8:00











                            • No. It's a pipe from wrapr package.

                              – Paweł Chabros
                              Jan 13 at 8:11






                            • 1





                              Why do you use %.>% in wrapr additionally instead of using %>% in tidyverse? They will get the same outcome in this case.

                              – Darren Tsai
                              Jan 13 at 8:19











                            • You are right. My bad.

                              – Paweł Chabros
                              Jan 13 at 8:58














                            1












                            1








                            1







                            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()





                            share|improve this answer













                            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()






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 13 at 7:29









                            Paweł ChabrosPaweł Chabros

                            2,014159




                            2,014159













                            • Is the 5th line a mistake %.>%?

                              – Khaynes
                              Jan 13 at 8:00











                            • No. It's a pipe from wrapr package.

                              – Paweł Chabros
                              Jan 13 at 8:11






                            • 1





                              Why do you use %.>% in wrapr additionally instead of using %>% in tidyverse? They will get the same outcome in this case.

                              – Darren Tsai
                              Jan 13 at 8:19











                            • You are right. My bad.

                              – Paweł Chabros
                              Jan 13 at 8:58



















                            • Is the 5th line a mistake %.>%?

                              – Khaynes
                              Jan 13 at 8:00











                            • No. It's a pipe from wrapr package.

                              – Paweł Chabros
                              Jan 13 at 8:11






                            • 1





                              Why do you use %.>% in wrapr additionally instead of using %>% in tidyverse? They will get the same outcome in this case.

                              – Darren Tsai
                              Jan 13 at 8:19











                            • You are right. My bad.

                              – Paweł Chabros
                              Jan 13 at 8:58

















                            Is the 5th line a mistake %.>%?

                            – Khaynes
                            Jan 13 at 8:00





                            Is the 5th line a mistake %.>%?

                            – Khaynes
                            Jan 13 at 8:00













                            No. It's a pipe from wrapr package.

                            – Paweł Chabros
                            Jan 13 at 8:11





                            No. It's a pipe from wrapr package.

                            – Paweł Chabros
                            Jan 13 at 8:11




                            1




                            1





                            Why do you use %.>% in wrapr additionally instead of using %>% in tidyverse? They will get the same outcome in this case.

                            – Darren Tsai
                            Jan 13 at 8:19





                            Why do you use %.>% in wrapr additionally instead of using %>% in tidyverse? They will get the same outcome in this case.

                            – Darren Tsai
                            Jan 13 at 8:19













                            You are right. My bad.

                            – Paweł Chabros
                            Jan 13 at 8:58





                            You are right. My bad.

                            – Paweł Chabros
                            Jan 13 at 8:58











                            1














                            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)





                            share|improve this answer






























                              1














                              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)





                              share|improve this answer




























                                1












                                1








                                1







                                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)





                                share|improve this answer















                                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)






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Jan 13 at 8:06

























                                answered Jan 13 at 7:54









                                Darren TsaiDarren Tsai

                                2,6572531




                                2,6572531























                                    0














                                    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





                                    share|improve this answer




























                                      0














                                      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





                                      share|improve this answer


























                                        0












                                        0








                                        0







                                        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





                                        share|improve this answer













                                        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






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Jan 14 at 3:41









                                        wwwwww

                                        28.8k112345




                                        28.8k112345























                                            0














                                            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)]))





                                            share|improve this answer






























                                              0














                                              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)]))





                                              share|improve this answer




























                                                0












                                                0








                                                0







                                                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)]))





                                                share|improve this answer















                                                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)]))






                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Jan 14 at 11:10

























                                                answered Jan 14 at 11:04









                                                Moody_MudskipperMoody_Mudskipper

                                                25k33673




                                                25k33673






























                                                    draft saved

                                                    draft discarded




















































                                                    Thanks for contributing an answer to Stack Overflow!


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid



                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.


                                                    To learn more, see our tips on writing great answers.




                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function () {
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54166704%2fhow-to-turn-a-list-inside-out%23new-answer', 'question_page');
                                                    }
                                                    );

                                                    Post as a guest















                                                    Required, but never shown





















































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown

































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown







                                                    Popular posts from this blog

                                                    Bressuire

                                                    Cabo Verde

                                                    Gyllenstierna