How to extract a vector from a list of vectors via matrix in R












1















Problem:



I have a number of vectors. I would like to sign numbers to these vectors starting from 1 to n. Then, I would like to convert these number to a lower.triangular matrix. After that, I would like to extract the vectors comes at the last row of the matrix.



Steps of the problem:




  1. Sign numbers n vectors from 1 to n.


  2. Convert these numbers to a lower.triangular matrix M.


  3. Then, extract the vectors that match the numbers at the last row of the matrix M.



Example:



Assume that I have a list of 10 vectors (X):



X <- list(x1=c(1:3), x2=(2:5), x3=c(4:2), x4=c(5:7), x5=c(12,34,54), x6=c(3:6), x7=c(3:6), x8=c(3,4,5), x9=c(44,56,7), x10=c(34,5,4))


Then, I would like to order them from 1 to 10, where 1 refers to the first vector, and so on. Then, I will have a vector of these numbers, say x = c(1:10). Then, I would like to convert it to a lower triangular matrix M.



 M <- matrix(0,5,5)

> M[lower.tri(M, diag=FALSE)] <- x
> M
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 0 0 0
[2,] 1 0 0 0 0
[3,] 2 5 0 0 0
[4,] 3 6 8 0 0
[5,] 4 7 9 10 0


Now, I would like to extract, the last row.



> tail(M, 1)
[,1] [,2] [,3] [,4] [,5]
[5,] 4 7 9 10 0

> newX <- as.vector(tail(M,1))
> newX
[1] 4 7 9 10 0


Now the wanted vectors (to be extracted from the whole vectors) are 4, 7, 9, and 10. In other words, I need to extract, x4, x7, x9, and x10.



Hence, I would like to extract the vectors match these number.



Any idea or help, please?










share|improve this question



























    1















    Problem:



    I have a number of vectors. I would like to sign numbers to these vectors starting from 1 to n. Then, I would like to convert these number to a lower.triangular matrix. After that, I would like to extract the vectors comes at the last row of the matrix.



    Steps of the problem:




    1. Sign numbers n vectors from 1 to n.


    2. Convert these numbers to a lower.triangular matrix M.


    3. Then, extract the vectors that match the numbers at the last row of the matrix M.



    Example:



    Assume that I have a list of 10 vectors (X):



    X <- list(x1=c(1:3), x2=(2:5), x3=c(4:2), x4=c(5:7), x5=c(12,34,54), x6=c(3:6), x7=c(3:6), x8=c(3,4,5), x9=c(44,56,7), x10=c(34,5,4))


    Then, I would like to order them from 1 to 10, where 1 refers to the first vector, and so on. Then, I will have a vector of these numbers, say x = c(1:10). Then, I would like to convert it to a lower triangular matrix M.



     M <- matrix(0,5,5)

    > M[lower.tri(M, diag=FALSE)] <- x
    > M
    [,1] [,2] [,3] [,4] [,5]
    [1,] 0 0 0 0 0
    [2,] 1 0 0 0 0
    [3,] 2 5 0 0 0
    [4,] 3 6 8 0 0
    [5,] 4 7 9 10 0


    Now, I would like to extract, the last row.



    > tail(M, 1)
    [,1] [,2] [,3] [,4] [,5]
    [5,] 4 7 9 10 0

    > newX <- as.vector(tail(M,1))
    > newX
    [1] 4 7 9 10 0


    Now the wanted vectors (to be extracted from the whole vectors) are 4, 7, 9, and 10. In other words, I need to extract, x4, x7, x9, and x10.



    Hence, I would like to extract the vectors match these number.



    Any idea or help, please?










    share|improve this question

























      1












      1








      1








      Problem:



      I have a number of vectors. I would like to sign numbers to these vectors starting from 1 to n. Then, I would like to convert these number to a lower.triangular matrix. After that, I would like to extract the vectors comes at the last row of the matrix.



      Steps of the problem:




      1. Sign numbers n vectors from 1 to n.


      2. Convert these numbers to a lower.triangular matrix M.


      3. Then, extract the vectors that match the numbers at the last row of the matrix M.



      Example:



      Assume that I have a list of 10 vectors (X):



      X <- list(x1=c(1:3), x2=(2:5), x3=c(4:2), x4=c(5:7), x5=c(12,34,54), x6=c(3:6), x7=c(3:6), x8=c(3,4,5), x9=c(44,56,7), x10=c(34,5,4))


      Then, I would like to order them from 1 to 10, where 1 refers to the first vector, and so on. Then, I will have a vector of these numbers, say x = c(1:10). Then, I would like to convert it to a lower triangular matrix M.



       M <- matrix(0,5,5)

      > M[lower.tri(M, diag=FALSE)] <- x
      > M
      [,1] [,2] [,3] [,4] [,5]
      [1,] 0 0 0 0 0
      [2,] 1 0 0 0 0
      [3,] 2 5 0 0 0
      [4,] 3 6 8 0 0
      [5,] 4 7 9 10 0


      Now, I would like to extract, the last row.



      > tail(M, 1)
      [,1] [,2] [,3] [,4] [,5]
      [5,] 4 7 9 10 0

      > newX <- as.vector(tail(M,1))
      > newX
      [1] 4 7 9 10 0


      Now the wanted vectors (to be extracted from the whole vectors) are 4, 7, 9, and 10. In other words, I need to extract, x4, x7, x9, and x10.



      Hence, I would like to extract the vectors match these number.



      Any idea or help, please?










      share|improve this question














      Problem:



      I have a number of vectors. I would like to sign numbers to these vectors starting from 1 to n. Then, I would like to convert these number to a lower.triangular matrix. After that, I would like to extract the vectors comes at the last row of the matrix.



      Steps of the problem:




      1. Sign numbers n vectors from 1 to n.


      2. Convert these numbers to a lower.triangular matrix M.


      3. Then, extract the vectors that match the numbers at the last row of the matrix M.



      Example:



      Assume that I have a list of 10 vectors (X):



      X <- list(x1=c(1:3), x2=(2:5), x3=c(4:2), x4=c(5:7), x5=c(12,34,54), x6=c(3:6), x7=c(3:6), x8=c(3,4,5), x9=c(44,56,7), x10=c(34,5,4))


      Then, I would like to order them from 1 to 10, where 1 refers to the first vector, and so on. Then, I will have a vector of these numbers, say x = c(1:10). Then, I would like to convert it to a lower triangular matrix M.



       M <- matrix(0,5,5)

      > M[lower.tri(M, diag=FALSE)] <- x
      > M
      [,1] [,2] [,3] [,4] [,5]
      [1,] 0 0 0 0 0
      [2,] 1 0 0 0 0
      [3,] 2 5 0 0 0
      [4,] 3 6 8 0 0
      [5,] 4 7 9 10 0


      Now, I would like to extract, the last row.



      > tail(M, 1)
      [,1] [,2] [,3] [,4] [,5]
      [5,] 4 7 9 10 0

      > newX <- as.vector(tail(M,1))
      > newX
      [1] 4 7 9 10 0


      Now the wanted vectors (to be extracted from the whole vectors) are 4, 7, 9, and 10. In other words, I need to extract, x4, x7, x9, and x10.



      Hence, I would like to extract the vectors match these number.



      Any idea or help, please?







      r






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 6:37









      MaryamMaryam

      28911




      28911
























          1 Answer
          1






          active

          oldest

          votes


















          2














          You could use paste:



          X[paste0("x", newX[newX != 0])]

          $`x4`
          [1] 5 6 7

          $x7
          [1] 3 4 5 6

          $x9
          [1] 44 56 7

          $x10
          [1] 34 5 4


          paste0("x", newX[newX != 0] will create the character vector "x4", "x7", "x9", "x10" which you can use for indexing the list.






          share|improve this answer





















          • 1





            Thank you so much for your very clever help. I thought I will need some lines of codes to done this.

            – Maryam
            Nov 15 '18 at 7:58






          • 1





            You are welcome! Often enough R surprises you with a really simple solution for a seemingly big problem :)

            – LAP
            Nov 15 '18 at 7:59











          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%2f53313746%2fhow-to-extract-a-vector-from-a-list-of-vectors-via-matrix-in-r%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          You could use paste:



          X[paste0("x", newX[newX != 0])]

          $`x4`
          [1] 5 6 7

          $x7
          [1] 3 4 5 6

          $x9
          [1] 44 56 7

          $x10
          [1] 34 5 4


          paste0("x", newX[newX != 0] will create the character vector "x4", "x7", "x9", "x10" which you can use for indexing the list.






          share|improve this answer





















          • 1





            Thank you so much for your very clever help. I thought I will need some lines of codes to done this.

            – Maryam
            Nov 15 '18 at 7:58






          • 1





            You are welcome! Often enough R surprises you with a really simple solution for a seemingly big problem :)

            – LAP
            Nov 15 '18 at 7:59
















          2














          You could use paste:



          X[paste0("x", newX[newX != 0])]

          $`x4`
          [1] 5 6 7

          $x7
          [1] 3 4 5 6

          $x9
          [1] 44 56 7

          $x10
          [1] 34 5 4


          paste0("x", newX[newX != 0] will create the character vector "x4", "x7", "x9", "x10" which you can use for indexing the list.






          share|improve this answer





















          • 1





            Thank you so much for your very clever help. I thought I will need some lines of codes to done this.

            – Maryam
            Nov 15 '18 at 7:58






          • 1





            You are welcome! Often enough R surprises you with a really simple solution for a seemingly big problem :)

            – LAP
            Nov 15 '18 at 7:59














          2












          2








          2







          You could use paste:



          X[paste0("x", newX[newX != 0])]

          $`x4`
          [1] 5 6 7

          $x7
          [1] 3 4 5 6

          $x9
          [1] 44 56 7

          $x10
          [1] 34 5 4


          paste0("x", newX[newX != 0] will create the character vector "x4", "x7", "x9", "x10" which you can use for indexing the list.






          share|improve this answer















          You could use paste:



          X[paste0("x", newX[newX != 0])]

          $`x4`
          [1] 5 6 7

          $x7
          [1] 3 4 5 6

          $x9
          [1] 44 56 7

          $x10
          [1] 34 5 4


          paste0("x", newX[newX != 0] will create the character vector "x4", "x7", "x9", "x10" which you can use for indexing the list.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 7:02

























          answered Nov 15 '18 at 6:56









          LAPLAP

          5,7202622




          5,7202622








          • 1





            Thank you so much for your very clever help. I thought I will need some lines of codes to done this.

            – Maryam
            Nov 15 '18 at 7:58






          • 1





            You are welcome! Often enough R surprises you with a really simple solution for a seemingly big problem :)

            – LAP
            Nov 15 '18 at 7:59














          • 1





            Thank you so much for your very clever help. I thought I will need some lines of codes to done this.

            – Maryam
            Nov 15 '18 at 7:58






          • 1





            You are welcome! Often enough R surprises you with a really simple solution for a seemingly big problem :)

            – LAP
            Nov 15 '18 at 7:59








          1




          1





          Thank you so much for your very clever help. I thought I will need some lines of codes to done this.

          – Maryam
          Nov 15 '18 at 7:58





          Thank you so much for your very clever help. I thought I will need some lines of codes to done this.

          – Maryam
          Nov 15 '18 at 7:58




          1




          1





          You are welcome! Often enough R surprises you with a really simple solution for a seemingly big problem :)

          – LAP
          Nov 15 '18 at 7:59





          You are welcome! Often enough R surprises you with a really simple solution for a seemingly big problem :)

          – LAP
          Nov 15 '18 at 7:59




















          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%2f53313746%2fhow-to-extract-a-vector-from-a-list-of-vectors-via-matrix-in-r%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Bressuire

          Vorschmack

          Quarantine