Convert list of values to a list of list indexes for each value












7















I have a list 'cats.list' with 6 elements. There are 9 unique integers that are members of one or more elements. E.g.



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


I want to create a list with one element for each of the 9 integers in 'cats.list'. Each element in the new list should contain the list indexes in 'cat.list' for a given integer.



For example, 1 occurs in the list elements 1, 2, 5 in 'cat.list'. 2 occurs in element 1 only. 3 occurs in element 3, 4, 5. So the first three element in the new list would be:



el.list <- list(c(1, 2, 5), 1, c(3, 4, 5)...) 


How can I create such a list of indexes for any 'cats.list'?










share|improve this question





























    7















    I have a list 'cats.list' with 6 elements. There are 9 unique integers that are members of one or more elements. E.g.



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


    I want to create a list with one element for each of the 9 integers in 'cats.list'. Each element in the new list should contain the list indexes in 'cat.list' for a given integer.



    For example, 1 occurs in the list elements 1, 2, 5 in 'cat.list'. 2 occurs in element 1 only. 3 occurs in element 3, 4, 5. So the first three element in the new list would be:



    el.list <- list(c(1, 2, 5), 1, c(3, 4, 5)...) 


    How can I create such a list of indexes for any 'cats.list'?










    share|improve this question



























      7












      7








      7


      1






      I have a list 'cats.list' with 6 elements. There are 9 unique integers that are members of one or more elements. E.g.



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


      I want to create a list with one element for each of the 9 integers in 'cats.list'. Each element in the new list should contain the list indexes in 'cat.list' for a given integer.



      For example, 1 occurs in the list elements 1, 2, 5 in 'cat.list'. 2 occurs in element 1 only. 3 occurs in element 3, 4, 5. So the first three element in the new list would be:



      el.list <- list(c(1, 2, 5), 1, c(3, 4, 5)...) 


      How can I create such a list of indexes for any 'cats.list'?










      share|improve this question
















      I have a list 'cats.list' with 6 elements. There are 9 unique integers that are members of one or more elements. E.g.



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


      I want to create a list with one element for each of the 9 integers in 'cats.list'. Each element in the new list should contain the list indexes in 'cat.list' for a given integer.



      For example, 1 occurs in the list elements 1, 2, 5 in 'cat.list'. 2 occurs in element 1 only. 3 occurs in element 3, 4, 5. So the first three element in the new list would be:



      el.list <- list(c(1, 2, 5), 1, c(3, 4, 5)...) 


      How can I create such a list of indexes for any 'cats.list'?







      r






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 24 '18 at 20:08









      Henrik

      42k994110




      42k994110










      asked Nov 24 '18 at 19:00









      SteveMSteveM

      190211




      190211
























          4 Answers
          4






          active

          oldest

          votes


















          5














          1) reshape2 Use melt in reshape2 to convert cats.list into a data frame whose first column value is the element and whose second column L1 is the corresponding component number in cats.list that that element belongs to. Then unstack that with the indicated formula.



          library(reshape2)

          unstack(melt(cats.list), L1 ~ value)


          giving:



          $`1`
          [1] 1 2 5

          $`2`
          [1] 1

          $`3`
          [1] 3 4 5

          $`4`
          [1] 3 6

          $`5`
          [1] 3 6

          $`6`
          [1] 1 4

          $`7`
          [1] 3 4 5

          $`8`
          [1] 2 5

          $`9`
          [1] 2 5 6


          2) split We could also do it this way without any packages. rep(seq_along(L), L) equals m$L1 from (1) and unlist(cats.list) equals m$value from (1).



          L <- lengths(cats.list)
          split(rep(seq_along(L), L), unlist(cats.list))


          3) stack/unstack We can also do this using only base R and stack/unstack if we name the cats.list components.



          cats.named <- setNames(cats.list, seq_along(cats.list))
          unstack(stack(cats.named), ind ~ values)


          Note



          We can plot this as a bipartite graph like this:



          library(igraph)
          library(reshape2)

          m <- melt(cats.list)
          M <- table(m)
          g <- graph_from_incidence_matrix(M)
          plot(g, layout = layout_as_bipartite)


          screenshot






          share|improve this answer


























          • That's a really clever use of split!

            – jdobres
            Nov 24 '18 at 20:26



















          5














          Use -



          cats.list <- list(c(1, 2, 6), c(1, 8, 9), c(3, 4, 5, 7), c(3, 6, 7), 
          c(1, 3, 7, 8, 9), c(4, 5, 9))
          output <- c()
          for(i in sort(unique(unlist(cats.list)))){
          output <- c(output, list(grep(i,cats.list)))
          }


          Output



          [[1]]
          [1] 1 2 5

          [[2]]
          [1] 1

          [[3]]
          [1] 3 4 5

          [[4]]
          [1] 3 6

          [[5]]
          [1] 3 6

          [[6]]
          [1] 1 4

          [[7]]
          [1] 3 4 5

          [[8]]
          [1] 2 5

          [[9]]
          [1] 2 5 6


          Explanation



          unlist(cats.list) flattens the existing list, wrapping it with unique and sort creates the search list with which you can iterate for the search



          The magic lies in grep(i,cats.list), which readily gives what you want for each search.



          Putting it together in an output list is trivial. Hope that helps!



          EDIT



          Thanks to @ G. Grothendieck, this can be shortened to --



          output <- lapply(sort(unique(unlist(cats.list))), grep, cats.list)





          share|improve this answer





















          • 1





            This could be shortened to: lapply(sort(unique(unlist(cats.list))), grep, cats.list)

            – G. Grothendieck
            Nov 25 '18 at 2:56











          • Cool suggestion! AFK ATM, will update in some time!

            – Vivek Kalyanarangan
            Nov 25 '18 at 4:06






          • 1





            Note that this would have to be modified if there were multi-digit elements in the components of cat.list since, for example, grep(1, list(1:9, 10:19)) returns 1,2 rather than just 1.

            – G. Grothendieck
            Nov 25 '18 at 21:55



















          1














          Just to round out the available options here, a version that uses two calls to sapply/lapply rather than a for loop and grep:



          sapply(sort(unique(unlist(cats.list))), function(x) {
          idx <- sapply(cats.list, function(y) any(y == x))
          return(which(idx))
          })

          [[1]]
          [1] 1 2 5

          [[2]]
          [1] 1

          [[3]]
          [1] 3 4 5

          [[4]]
          [1] 3 6

          [[5]]
          [1] 3 6

          [[6]]
          [1] 1 4

          [[7]]
          [1] 3 4 5

          [[8]]
          [1] 2 5

          [[9]]
          [1] 2 5 6





          share|improve this answer































            0














            A tidyverse version :



            tibble(cats.list) %>% 
            rowid_to_column() %>%
            unnest %>%
            group_by(cats.list) %>%
            summarize_at("rowid", list) %>%
            pull(rowid)
            # [[1]]
            # [1] 1 2 5
            #
            # [[2]]
            # [1] 1
            #
            # [[3]]
            # [1] 3 4 5
            #
            # [[4]]
            # [1] 3 6
            #
            # [[5]]
            # [1] 3 6
            #
            # [[6]]
            # [1] 1 4
            #
            # [[7]]
            # [1] 3 4 5
            #
            # [[8]]
            # [1] 2 5
            #
            # [[9]]
            # [1] 2 5 6
            #





            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%2f53461430%2fconvert-list-of-values-to-a-list-of-list-indexes-for-each-value%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              5














              1) reshape2 Use melt in reshape2 to convert cats.list into a data frame whose first column value is the element and whose second column L1 is the corresponding component number in cats.list that that element belongs to. Then unstack that with the indicated formula.



              library(reshape2)

              unstack(melt(cats.list), L1 ~ value)


              giving:



              $`1`
              [1] 1 2 5

              $`2`
              [1] 1

              $`3`
              [1] 3 4 5

              $`4`
              [1] 3 6

              $`5`
              [1] 3 6

              $`6`
              [1] 1 4

              $`7`
              [1] 3 4 5

              $`8`
              [1] 2 5

              $`9`
              [1] 2 5 6


              2) split We could also do it this way without any packages. rep(seq_along(L), L) equals m$L1 from (1) and unlist(cats.list) equals m$value from (1).



              L <- lengths(cats.list)
              split(rep(seq_along(L), L), unlist(cats.list))


              3) stack/unstack We can also do this using only base R and stack/unstack if we name the cats.list components.



              cats.named <- setNames(cats.list, seq_along(cats.list))
              unstack(stack(cats.named), ind ~ values)


              Note



              We can plot this as a bipartite graph like this:



              library(igraph)
              library(reshape2)

              m <- melt(cats.list)
              M <- table(m)
              g <- graph_from_incidence_matrix(M)
              plot(g, layout = layout_as_bipartite)


              screenshot






              share|improve this answer


























              • That's a really clever use of split!

                – jdobres
                Nov 24 '18 at 20:26
















              5














              1) reshape2 Use melt in reshape2 to convert cats.list into a data frame whose first column value is the element and whose second column L1 is the corresponding component number in cats.list that that element belongs to. Then unstack that with the indicated formula.



              library(reshape2)

              unstack(melt(cats.list), L1 ~ value)


              giving:



              $`1`
              [1] 1 2 5

              $`2`
              [1] 1

              $`3`
              [1] 3 4 5

              $`4`
              [1] 3 6

              $`5`
              [1] 3 6

              $`6`
              [1] 1 4

              $`7`
              [1] 3 4 5

              $`8`
              [1] 2 5

              $`9`
              [1] 2 5 6


              2) split We could also do it this way without any packages. rep(seq_along(L), L) equals m$L1 from (1) and unlist(cats.list) equals m$value from (1).



              L <- lengths(cats.list)
              split(rep(seq_along(L), L), unlist(cats.list))


              3) stack/unstack We can also do this using only base R and stack/unstack if we name the cats.list components.



              cats.named <- setNames(cats.list, seq_along(cats.list))
              unstack(stack(cats.named), ind ~ values)


              Note



              We can plot this as a bipartite graph like this:



              library(igraph)
              library(reshape2)

              m <- melt(cats.list)
              M <- table(m)
              g <- graph_from_incidence_matrix(M)
              plot(g, layout = layout_as_bipartite)


              screenshot






              share|improve this answer


























              • That's a really clever use of split!

                – jdobres
                Nov 24 '18 at 20:26














              5












              5








              5







              1) reshape2 Use melt in reshape2 to convert cats.list into a data frame whose first column value is the element and whose second column L1 is the corresponding component number in cats.list that that element belongs to. Then unstack that with the indicated formula.



              library(reshape2)

              unstack(melt(cats.list), L1 ~ value)


              giving:



              $`1`
              [1] 1 2 5

              $`2`
              [1] 1

              $`3`
              [1] 3 4 5

              $`4`
              [1] 3 6

              $`5`
              [1] 3 6

              $`6`
              [1] 1 4

              $`7`
              [1] 3 4 5

              $`8`
              [1] 2 5

              $`9`
              [1] 2 5 6


              2) split We could also do it this way without any packages. rep(seq_along(L), L) equals m$L1 from (1) and unlist(cats.list) equals m$value from (1).



              L <- lengths(cats.list)
              split(rep(seq_along(L), L), unlist(cats.list))


              3) stack/unstack We can also do this using only base R and stack/unstack if we name the cats.list components.



              cats.named <- setNames(cats.list, seq_along(cats.list))
              unstack(stack(cats.named), ind ~ values)


              Note



              We can plot this as a bipartite graph like this:



              library(igraph)
              library(reshape2)

              m <- melt(cats.list)
              M <- table(m)
              g <- graph_from_incidence_matrix(M)
              plot(g, layout = layout_as_bipartite)


              screenshot






              share|improve this answer















              1) reshape2 Use melt in reshape2 to convert cats.list into a data frame whose first column value is the element and whose second column L1 is the corresponding component number in cats.list that that element belongs to. Then unstack that with the indicated formula.



              library(reshape2)

              unstack(melt(cats.list), L1 ~ value)


              giving:



              $`1`
              [1] 1 2 5

              $`2`
              [1] 1

              $`3`
              [1] 3 4 5

              $`4`
              [1] 3 6

              $`5`
              [1] 3 6

              $`6`
              [1] 1 4

              $`7`
              [1] 3 4 5

              $`8`
              [1] 2 5

              $`9`
              [1] 2 5 6


              2) split We could also do it this way without any packages. rep(seq_along(L), L) equals m$L1 from (1) and unlist(cats.list) equals m$value from (1).



              L <- lengths(cats.list)
              split(rep(seq_along(L), L), unlist(cats.list))


              3) stack/unstack We can also do this using only base R and stack/unstack if we name the cats.list components.



              cats.named <- setNames(cats.list, seq_along(cats.list))
              unstack(stack(cats.named), ind ~ values)


              Note



              We can plot this as a bipartite graph like this:



              library(igraph)
              library(reshape2)

              m <- melt(cats.list)
              M <- table(m)
              g <- graph_from_incidence_matrix(M)
              plot(g, layout = layout_as_bipartite)


              screenshot







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 25 '18 at 2:48

























              answered Nov 24 '18 at 20:13









              G. GrothendieckG. Grothendieck

              151k10134239




              151k10134239













              • That's a really clever use of split!

                – jdobres
                Nov 24 '18 at 20:26



















              • That's a really clever use of split!

                – jdobres
                Nov 24 '18 at 20:26

















              That's a really clever use of split!

              – jdobres
              Nov 24 '18 at 20:26





              That's a really clever use of split!

              – jdobres
              Nov 24 '18 at 20:26













              5














              Use -



              cats.list <- list(c(1, 2, 6), c(1, 8, 9), c(3, 4, 5, 7), c(3, 6, 7), 
              c(1, 3, 7, 8, 9), c(4, 5, 9))
              output <- c()
              for(i in sort(unique(unlist(cats.list)))){
              output <- c(output, list(grep(i,cats.list)))
              }


              Output



              [[1]]
              [1] 1 2 5

              [[2]]
              [1] 1

              [[3]]
              [1] 3 4 5

              [[4]]
              [1] 3 6

              [[5]]
              [1] 3 6

              [[6]]
              [1] 1 4

              [[7]]
              [1] 3 4 5

              [[8]]
              [1] 2 5

              [[9]]
              [1] 2 5 6


              Explanation



              unlist(cats.list) flattens the existing list, wrapping it with unique and sort creates the search list with which you can iterate for the search



              The magic lies in grep(i,cats.list), which readily gives what you want for each search.



              Putting it together in an output list is trivial. Hope that helps!



              EDIT



              Thanks to @ G. Grothendieck, this can be shortened to --



              output <- lapply(sort(unique(unlist(cats.list))), grep, cats.list)





              share|improve this answer





















              • 1





                This could be shortened to: lapply(sort(unique(unlist(cats.list))), grep, cats.list)

                – G. Grothendieck
                Nov 25 '18 at 2:56











              • Cool suggestion! AFK ATM, will update in some time!

                – Vivek Kalyanarangan
                Nov 25 '18 at 4:06






              • 1





                Note that this would have to be modified if there were multi-digit elements in the components of cat.list since, for example, grep(1, list(1:9, 10:19)) returns 1,2 rather than just 1.

                – G. Grothendieck
                Nov 25 '18 at 21:55
















              5














              Use -



              cats.list <- list(c(1, 2, 6), c(1, 8, 9), c(3, 4, 5, 7), c(3, 6, 7), 
              c(1, 3, 7, 8, 9), c(4, 5, 9))
              output <- c()
              for(i in sort(unique(unlist(cats.list)))){
              output <- c(output, list(grep(i,cats.list)))
              }


              Output



              [[1]]
              [1] 1 2 5

              [[2]]
              [1] 1

              [[3]]
              [1] 3 4 5

              [[4]]
              [1] 3 6

              [[5]]
              [1] 3 6

              [[6]]
              [1] 1 4

              [[7]]
              [1] 3 4 5

              [[8]]
              [1] 2 5

              [[9]]
              [1] 2 5 6


              Explanation



              unlist(cats.list) flattens the existing list, wrapping it with unique and sort creates the search list with which you can iterate for the search



              The magic lies in grep(i,cats.list), which readily gives what you want for each search.



              Putting it together in an output list is trivial. Hope that helps!



              EDIT



              Thanks to @ G. Grothendieck, this can be shortened to --



              output <- lapply(sort(unique(unlist(cats.list))), grep, cats.list)





              share|improve this answer





















              • 1





                This could be shortened to: lapply(sort(unique(unlist(cats.list))), grep, cats.list)

                – G. Grothendieck
                Nov 25 '18 at 2:56











              • Cool suggestion! AFK ATM, will update in some time!

                – Vivek Kalyanarangan
                Nov 25 '18 at 4:06






              • 1





                Note that this would have to be modified if there were multi-digit elements in the components of cat.list since, for example, grep(1, list(1:9, 10:19)) returns 1,2 rather than just 1.

                – G. Grothendieck
                Nov 25 '18 at 21:55














              5












              5








              5







              Use -



              cats.list <- list(c(1, 2, 6), c(1, 8, 9), c(3, 4, 5, 7), c(3, 6, 7), 
              c(1, 3, 7, 8, 9), c(4, 5, 9))
              output <- c()
              for(i in sort(unique(unlist(cats.list)))){
              output <- c(output, list(grep(i,cats.list)))
              }


              Output



              [[1]]
              [1] 1 2 5

              [[2]]
              [1] 1

              [[3]]
              [1] 3 4 5

              [[4]]
              [1] 3 6

              [[5]]
              [1] 3 6

              [[6]]
              [1] 1 4

              [[7]]
              [1] 3 4 5

              [[8]]
              [1] 2 5

              [[9]]
              [1] 2 5 6


              Explanation



              unlist(cats.list) flattens the existing list, wrapping it with unique and sort creates the search list with which you can iterate for the search



              The magic lies in grep(i,cats.list), which readily gives what you want for each search.



              Putting it together in an output list is trivial. Hope that helps!



              EDIT



              Thanks to @ G. Grothendieck, this can be shortened to --



              output <- lapply(sort(unique(unlist(cats.list))), grep, cats.list)





              share|improve this answer















              Use -



              cats.list <- list(c(1, 2, 6), c(1, 8, 9), c(3, 4, 5, 7), c(3, 6, 7), 
              c(1, 3, 7, 8, 9), c(4, 5, 9))
              output <- c()
              for(i in sort(unique(unlist(cats.list)))){
              output <- c(output, list(grep(i,cats.list)))
              }


              Output



              [[1]]
              [1] 1 2 5

              [[2]]
              [1] 1

              [[3]]
              [1] 3 4 5

              [[4]]
              [1] 3 6

              [[5]]
              [1] 3 6

              [[6]]
              [1] 1 4

              [[7]]
              [1] 3 4 5

              [[8]]
              [1] 2 5

              [[9]]
              [1] 2 5 6


              Explanation



              unlist(cats.list) flattens the existing list, wrapping it with unique and sort creates the search list with which you can iterate for the search



              The magic lies in grep(i,cats.list), which readily gives what you want for each search.



              Putting it together in an output list is trivial. Hope that helps!



              EDIT



              Thanks to @ G. Grothendieck, this can be shortened to --



              output <- lapply(sort(unique(unlist(cats.list))), grep, cats.list)






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 25 '18 at 5:14

























              answered Nov 24 '18 at 19:34









              Vivek KalyanaranganVivek Kalyanarangan

              5,1041829




              5,1041829








              • 1





                This could be shortened to: lapply(sort(unique(unlist(cats.list))), grep, cats.list)

                – G. Grothendieck
                Nov 25 '18 at 2:56











              • Cool suggestion! AFK ATM, will update in some time!

                – Vivek Kalyanarangan
                Nov 25 '18 at 4:06






              • 1





                Note that this would have to be modified if there were multi-digit elements in the components of cat.list since, for example, grep(1, list(1:9, 10:19)) returns 1,2 rather than just 1.

                – G. Grothendieck
                Nov 25 '18 at 21:55














              • 1





                This could be shortened to: lapply(sort(unique(unlist(cats.list))), grep, cats.list)

                – G. Grothendieck
                Nov 25 '18 at 2:56











              • Cool suggestion! AFK ATM, will update in some time!

                – Vivek Kalyanarangan
                Nov 25 '18 at 4:06






              • 1





                Note that this would have to be modified if there were multi-digit elements in the components of cat.list since, for example, grep(1, list(1:9, 10:19)) returns 1,2 rather than just 1.

                – G. Grothendieck
                Nov 25 '18 at 21:55








              1




              1





              This could be shortened to: lapply(sort(unique(unlist(cats.list))), grep, cats.list)

              – G. Grothendieck
              Nov 25 '18 at 2:56





              This could be shortened to: lapply(sort(unique(unlist(cats.list))), grep, cats.list)

              – G. Grothendieck
              Nov 25 '18 at 2:56













              Cool suggestion! AFK ATM, will update in some time!

              – Vivek Kalyanarangan
              Nov 25 '18 at 4:06





              Cool suggestion! AFK ATM, will update in some time!

              – Vivek Kalyanarangan
              Nov 25 '18 at 4:06




              1




              1





              Note that this would have to be modified if there were multi-digit elements in the components of cat.list since, for example, grep(1, list(1:9, 10:19)) returns 1,2 rather than just 1.

              – G. Grothendieck
              Nov 25 '18 at 21:55





              Note that this would have to be modified if there were multi-digit elements in the components of cat.list since, for example, grep(1, list(1:9, 10:19)) returns 1,2 rather than just 1.

              – G. Grothendieck
              Nov 25 '18 at 21:55











              1














              Just to round out the available options here, a version that uses two calls to sapply/lapply rather than a for loop and grep:



              sapply(sort(unique(unlist(cats.list))), function(x) {
              idx <- sapply(cats.list, function(y) any(y == x))
              return(which(idx))
              })

              [[1]]
              [1] 1 2 5

              [[2]]
              [1] 1

              [[3]]
              [1] 3 4 5

              [[4]]
              [1] 3 6

              [[5]]
              [1] 3 6

              [[6]]
              [1] 1 4

              [[7]]
              [1] 3 4 5

              [[8]]
              [1] 2 5

              [[9]]
              [1] 2 5 6





              share|improve this answer




























                1














                Just to round out the available options here, a version that uses two calls to sapply/lapply rather than a for loop and grep:



                sapply(sort(unique(unlist(cats.list))), function(x) {
                idx <- sapply(cats.list, function(y) any(y == x))
                return(which(idx))
                })

                [[1]]
                [1] 1 2 5

                [[2]]
                [1] 1

                [[3]]
                [1] 3 4 5

                [[4]]
                [1] 3 6

                [[5]]
                [1] 3 6

                [[6]]
                [1] 1 4

                [[7]]
                [1] 3 4 5

                [[8]]
                [1] 2 5

                [[9]]
                [1] 2 5 6





                share|improve this answer


























                  1












                  1








                  1







                  Just to round out the available options here, a version that uses two calls to sapply/lapply rather than a for loop and grep:



                  sapply(sort(unique(unlist(cats.list))), function(x) {
                  idx <- sapply(cats.list, function(y) any(y == x))
                  return(which(idx))
                  })

                  [[1]]
                  [1] 1 2 5

                  [[2]]
                  [1] 1

                  [[3]]
                  [1] 3 4 5

                  [[4]]
                  [1] 3 6

                  [[5]]
                  [1] 3 6

                  [[6]]
                  [1] 1 4

                  [[7]]
                  [1] 3 4 5

                  [[8]]
                  [1] 2 5

                  [[9]]
                  [1] 2 5 6





                  share|improve this answer













                  Just to round out the available options here, a version that uses two calls to sapply/lapply rather than a for loop and grep:



                  sapply(sort(unique(unlist(cats.list))), function(x) {
                  idx <- sapply(cats.list, function(y) any(y == x))
                  return(which(idx))
                  })

                  [[1]]
                  [1] 1 2 5

                  [[2]]
                  [1] 1

                  [[3]]
                  [1] 3 4 5

                  [[4]]
                  [1] 3 6

                  [[5]]
                  [1] 3 6

                  [[6]]
                  [1] 1 4

                  [[7]]
                  [1] 3 4 5

                  [[8]]
                  [1] 2 5

                  [[9]]
                  [1] 2 5 6






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 24 '18 at 20:28









                  jdobresjdobres

                  4,9581522




                  4,9581522























                      0














                      A tidyverse version :



                      tibble(cats.list) %>% 
                      rowid_to_column() %>%
                      unnest %>%
                      group_by(cats.list) %>%
                      summarize_at("rowid", list) %>%
                      pull(rowid)
                      # [[1]]
                      # [1] 1 2 5
                      #
                      # [[2]]
                      # [1] 1
                      #
                      # [[3]]
                      # [1] 3 4 5
                      #
                      # [[4]]
                      # [1] 3 6
                      #
                      # [[5]]
                      # [1] 3 6
                      #
                      # [[6]]
                      # [1] 1 4
                      #
                      # [[7]]
                      # [1] 3 4 5
                      #
                      # [[8]]
                      # [1] 2 5
                      #
                      # [[9]]
                      # [1] 2 5 6
                      #





                      share|improve this answer




























                        0














                        A tidyverse version :



                        tibble(cats.list) %>% 
                        rowid_to_column() %>%
                        unnest %>%
                        group_by(cats.list) %>%
                        summarize_at("rowid", list) %>%
                        pull(rowid)
                        # [[1]]
                        # [1] 1 2 5
                        #
                        # [[2]]
                        # [1] 1
                        #
                        # [[3]]
                        # [1] 3 4 5
                        #
                        # [[4]]
                        # [1] 3 6
                        #
                        # [[5]]
                        # [1] 3 6
                        #
                        # [[6]]
                        # [1] 1 4
                        #
                        # [[7]]
                        # [1] 3 4 5
                        #
                        # [[8]]
                        # [1] 2 5
                        #
                        # [[9]]
                        # [1] 2 5 6
                        #





                        share|improve this answer


























                          0












                          0








                          0







                          A tidyverse version :



                          tibble(cats.list) %>% 
                          rowid_to_column() %>%
                          unnest %>%
                          group_by(cats.list) %>%
                          summarize_at("rowid", list) %>%
                          pull(rowid)
                          # [[1]]
                          # [1] 1 2 5
                          #
                          # [[2]]
                          # [1] 1
                          #
                          # [[3]]
                          # [1] 3 4 5
                          #
                          # [[4]]
                          # [1] 3 6
                          #
                          # [[5]]
                          # [1] 3 6
                          #
                          # [[6]]
                          # [1] 1 4
                          #
                          # [[7]]
                          # [1] 3 4 5
                          #
                          # [[8]]
                          # [1] 2 5
                          #
                          # [[9]]
                          # [1] 2 5 6
                          #





                          share|improve this answer













                          A tidyverse version :



                          tibble(cats.list) %>% 
                          rowid_to_column() %>%
                          unnest %>%
                          group_by(cats.list) %>%
                          summarize_at("rowid", list) %>%
                          pull(rowid)
                          # [[1]]
                          # [1] 1 2 5
                          #
                          # [[2]]
                          # [1] 1
                          #
                          # [[3]]
                          # [1] 3 4 5
                          #
                          # [[4]]
                          # [1] 3 6
                          #
                          # [[5]]
                          # [1] 3 6
                          #
                          # [[6]]
                          # [1] 1 4
                          #
                          # [[7]]
                          # [1] 3 4 5
                          #
                          # [[8]]
                          # [1] 2 5
                          #
                          # [[9]]
                          # [1] 2 5 6
                          #






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 26 '18 at 0:24









                          Moody_MudskipperMoody_Mudskipper

                          23.6k33265




                          23.6k33265






























                              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%2f53461430%2fconvert-list-of-values-to-a-list-of-list-indexes-for-each-value%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

                              Wiesbaden

                              Marschland

                              Dieringhausen