How to turn a list inside out?












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
    2 days ago











  • @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
    2 days ago






  • 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
    2 days ago
















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
    2 days ago











  • @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
    2 days ago






  • 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
    2 days ago














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 2 days ago









jakesjakes

409311




409311













  • 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
    2 days ago











  • @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
    2 days ago






  • 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
    2 days ago



















  • 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
    2 days ago











  • @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
    2 days ago






  • 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
    2 days ago

















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
2 days ago





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
2 days ago













@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
2 days ago





@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
2 days ago




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
2 days ago





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
2 days ago












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
    2 days ago








  • 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
    2 days ago













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

    – Khaynes
    yesterday



















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








      New contributor




      Paweł Chabros is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





















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

        – Khaynes
        2 days ago











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

        – Paweł Chabros
        2 days ago






      • 1





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

        – Darren Tsai
        2 days ago











      • You are right. My bad.

        – Paweł Chabros
        2 days ago



















      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
              2 days ago








            • 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
              2 days ago













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

              – Khaynes
              yesterday
















            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
              2 days ago








            • 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
              2 days ago













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

              – Khaynes
              yesterday














            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 yesterday

























            answered 2 days ago









            KhaynesKhaynes

            441417




            441417








            • 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
              2 days ago








            • 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
              2 days ago













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

              – Khaynes
              yesterday














            • 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
              2 days ago








            • 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
              2 days ago













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

              – Khaynes
              yesterday








            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
            2 days ago







            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
            2 days ago






            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
            2 days ago







            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
            2 days ago















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

            – Khaynes
            yesterday





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

            – Khaynes
            yesterday













            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 2 days ago









                vaettchenvaettchen

                5,1701332




                5,1701332























                    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 2 days ago

























                        answered 2 days ago









                        natenate

                        1,862316




                        1,862316























                            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








                            New contributor




                            Paweł Chabros is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.





















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

                              – Khaynes
                              2 days ago











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

                              – Paweł Chabros
                              2 days ago






                            • 1





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

                              – Darren Tsai
                              2 days ago











                            • You are right. My bad.

                              – Paweł Chabros
                              2 days ago
















                            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








                            New contributor




                            Paweł Chabros is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.





















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

                              – Khaynes
                              2 days ago











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

                              – Paweł Chabros
                              2 days ago






                            • 1





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

                              – Darren Tsai
                              2 days ago











                            • You are right. My bad.

                              – Paweł Chabros
                              2 days ago














                            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








                            New contributor




                            Paweł Chabros is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.










                            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








                            New contributor




                            Paweł Chabros is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.









                            share|improve this answer



                            share|improve this answer






                            New contributor




                            Paweł Chabros is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.









                            answered 2 days ago









                            Paweł ChabrosPaweł Chabros

                            28415




                            28415




                            New contributor




                            Paweł Chabros is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.





                            New contributor





                            Paweł Chabros is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.






                            Paweł Chabros is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.













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

                              – Khaynes
                              2 days ago











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

                              – Paweł Chabros
                              2 days ago






                            • 1





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

                              – Darren Tsai
                              2 days ago











                            • You are right. My bad.

                              – Paweł Chabros
                              2 days ago



















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

                              – Khaynes
                              2 days ago











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

                              – Paweł Chabros
                              2 days ago






                            • 1





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

                              – Darren Tsai
                              2 days ago











                            • You are right. My bad.

                              – Paweł Chabros
                              2 days ago

















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

                            – Khaynes
                            2 days ago





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

                            – Khaynes
                            2 days ago













                            No. It's a pipe from wrapr package.

                            – Paweł Chabros
                            2 days ago





                            No. It's a pipe from wrapr package.

                            – Paweł Chabros
                            2 days ago




                            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
                            2 days ago





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

                            – Darren Tsai
                            2 days ago













                            You are right. My bad.

                            – Paweł Chabros
                            2 days ago





                            You are right. My bad.

                            – Paweł Chabros
                            2 days ago











                            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 2 days ago

























                                answered 2 days ago









                                Darren TsaiDarren Tsai

                                1,540321




                                1,540321























                                    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 yesterday









                                        wwwwww

                                        26.1k112240




                                        26.1k112240























                                            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 yesterday

























                                                answered yesterday









                                                Moody_MudskipperMoody_Mudskipper

                                                21.7k32864




                                                21.7k32864






























                                                    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

                                                    "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                                                    Alcedinidae

                                                    RAC Tourist Trophy