How can I create a matrix , with random number on row and not replace,but in col can replace, R language





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







0















How can I create a matrix , with random number on row and not replace.
like this



 5 29 24 20 31 33    
2 18 35 4 11 21
30 40 22 14 2 28
33 14 4 18 5 10
10 33 15 2 28 18
7 22 9 25 31 20
12 29 31 22 37 26
7 31 34 28 19 23
7 34 11 6 31 28


my code :



matrix(sample(1:42, 60, replace = FALSE), ncol = 6)


But I receive this error message:




Error in sample.int(length(x), size, replace, prob) : cannot take a
sample larger than the population when 'replace = FALSE'




but it's wrong because only 1~42, it can't create a 60 matrix.










share|improve this question




















  • 3





    Do it per row? t(sapply(1:10, function(x) sample(1:42, 6, replace = FALSE)))

    – Jav
    Nov 23 '18 at 14:45













  • can use replicate maybe: matrix(replicate(2, sample(1:42, replace = FALSE)), ncol=6, nrow=10)

    – Jonny Phelps
    Nov 23 '18 at 15:12


















0















How can I create a matrix , with random number on row and not replace.
like this



 5 29 24 20 31 33    
2 18 35 4 11 21
30 40 22 14 2 28
33 14 4 18 5 10
10 33 15 2 28 18
7 22 9 25 31 20
12 29 31 22 37 26
7 31 34 28 19 23
7 34 11 6 31 28


my code :



matrix(sample(1:42, 60, replace = FALSE), ncol = 6)


But I receive this error message:




Error in sample.int(length(x), size, replace, prob) : cannot take a
sample larger than the population when 'replace = FALSE'




but it's wrong because only 1~42, it can't create a 60 matrix.










share|improve this question




















  • 3





    Do it per row? t(sapply(1:10, function(x) sample(1:42, 6, replace = FALSE)))

    – Jav
    Nov 23 '18 at 14:45













  • can use replicate maybe: matrix(replicate(2, sample(1:42, replace = FALSE)), ncol=6, nrow=10)

    – Jonny Phelps
    Nov 23 '18 at 15:12














0












0








0








How can I create a matrix , with random number on row and not replace.
like this



 5 29 24 20 31 33    
2 18 35 4 11 21
30 40 22 14 2 28
33 14 4 18 5 10
10 33 15 2 28 18
7 22 9 25 31 20
12 29 31 22 37 26
7 31 34 28 19 23
7 34 11 6 31 28


my code :



matrix(sample(1:42, 60, replace = FALSE), ncol = 6)


But I receive this error message:




Error in sample.int(length(x), size, replace, prob) : cannot take a
sample larger than the population when 'replace = FALSE'




but it's wrong because only 1~42, it can't create a 60 matrix.










share|improve this question
















How can I create a matrix , with random number on row and not replace.
like this



 5 29 24 20 31 33    
2 18 35 4 11 21
30 40 22 14 2 28
33 14 4 18 5 10
10 33 15 2 28 18
7 22 9 25 31 20
12 29 31 22 37 26
7 31 34 28 19 23
7 34 11 6 31 28


my code :



matrix(sample(1:42, 60, replace = FALSE), ncol = 6)


But I receive this error message:




Error in sample.int(length(x), size, replace, prob) : cannot take a
sample larger than the population when 'replace = FALSE'




but it's wrong because only 1~42, it can't create a 60 matrix.







r






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 15:57









alepuzio

1,19722732




1,19722732










asked Nov 23 '18 at 14:41









破嘎獸破嘎獸

173




173








  • 3





    Do it per row? t(sapply(1:10, function(x) sample(1:42, 6, replace = FALSE)))

    – Jav
    Nov 23 '18 at 14:45













  • can use replicate maybe: matrix(replicate(2, sample(1:42, replace = FALSE)), ncol=6, nrow=10)

    – Jonny Phelps
    Nov 23 '18 at 15:12














  • 3





    Do it per row? t(sapply(1:10, function(x) sample(1:42, 6, replace = FALSE)))

    – Jav
    Nov 23 '18 at 14:45













  • can use replicate maybe: matrix(replicate(2, sample(1:42, replace = FALSE)), ncol=6, nrow=10)

    – Jonny Phelps
    Nov 23 '18 at 15:12








3




3





Do it per row? t(sapply(1:10, function(x) sample(1:42, 6, replace = FALSE)))

– Jav
Nov 23 '18 at 14:45







Do it per row? t(sapply(1:10, function(x) sample(1:42, 6, replace = FALSE)))

– Jav
Nov 23 '18 at 14:45















can use replicate maybe: matrix(replicate(2, sample(1:42, replace = FALSE)), ncol=6, nrow=10)

– Jonny Phelps
Nov 23 '18 at 15:12





can use replicate maybe: matrix(replicate(2, sample(1:42, replace = FALSE)), ncol=6, nrow=10)

– Jonny Phelps
Nov 23 '18 at 15:12












2 Answers
2






active

oldest

votes


















1














You can not generate all 60 of the numbers with one sample function as you want to allow replacement of numbers in a different row. Therefore you have to do one sample per row. @Jav provided very neat code to accomplish this in the comment to the question:



t(sapply(1:10, function(x) sample(1:42, 6, replace = FALSE)))





share|improve this answer































    0














    if you want to have a different sample in each row, then replicate can help you -- but replicate (as pretty much everything else in R) works naturally columnwise, so you have to transpose the result:



    t(replicate(10, sample(1:42, 6)))




    • replace = FALSE is the default, so I didn't include it

    • after transposing, 10 becomes the number of rows and 6 becomes the number of columns






    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%2f53448709%2fhow-can-i-create-a-matrix-with-random-number-on-row-and-not-replace-but-in-col%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      You can not generate all 60 of the numbers with one sample function as you want to allow replacement of numbers in a different row. Therefore you have to do one sample per row. @Jav provided very neat code to accomplish this in the comment to the question:



      t(sapply(1:10, function(x) sample(1:42, 6, replace = FALSE)))





      share|improve this answer




























        1














        You can not generate all 60 of the numbers with one sample function as you want to allow replacement of numbers in a different row. Therefore you have to do one sample per row. @Jav provided very neat code to accomplish this in the comment to the question:



        t(sapply(1:10, function(x) sample(1:42, 6, replace = FALSE)))





        share|improve this answer


























          1












          1








          1







          You can not generate all 60 of the numbers with one sample function as you want to allow replacement of numbers in a different row. Therefore you have to do one sample per row. @Jav provided very neat code to accomplish this in the comment to the question:



          t(sapply(1:10, function(x) sample(1:42, 6, replace = FALSE)))





          share|improve this answer













          You can not generate all 60 of the numbers with one sample function as you want to allow replacement of numbers in a different row. Therefore you have to do one sample per row. @Jav provided very neat code to accomplish this in the comment to the question:



          t(sapply(1:10, function(x) sample(1:42, 6, replace = FALSE)))






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 15:39









          Aaro ViertiöAaro Viertiö

          1295




          1295

























              0














              if you want to have a different sample in each row, then replicate can help you -- but replicate (as pretty much everything else in R) works naturally columnwise, so you have to transpose the result:



              t(replicate(10, sample(1:42, 6)))




              • replace = FALSE is the default, so I didn't include it

              • after transposing, 10 becomes the number of rows and 6 becomes the number of columns






              share|improve this answer




























                0














                if you want to have a different sample in each row, then replicate can help you -- but replicate (as pretty much everything else in R) works naturally columnwise, so you have to transpose the result:



                t(replicate(10, sample(1:42, 6)))




                • replace = FALSE is the default, so I didn't include it

                • after transposing, 10 becomes the number of rows and 6 becomes the number of columns






                share|improve this answer


























                  0












                  0








                  0







                  if you want to have a different sample in each row, then replicate can help you -- but replicate (as pretty much everything else in R) works naturally columnwise, so you have to transpose the result:



                  t(replicate(10, sample(1:42, 6)))




                  • replace = FALSE is the default, so I didn't include it

                  • after transposing, 10 becomes the number of rows and 6 becomes the number of columns






                  share|improve this answer













                  if you want to have a different sample in each row, then replicate can help you -- but replicate (as pretty much everything else in R) works naturally columnwise, so you have to transpose the result:



                  t(replicate(10, sample(1:42, 6)))




                  • replace = FALSE is the default, so I didn't include it

                  • after transposing, 10 becomes the number of rows and 6 becomes the number of columns







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '18 at 15:53









                  lebatsnoklebatsnok

                  4,46621118




                  4,46621118






























                      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%2f53448709%2fhow-can-i-create-a-matrix-with-random-number-on-row-and-not-replace-but-in-col%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







                      這個網誌中的熱門文章

                      Academy of Television Arts & Sciences

                      L'Équipe

                      1995 France bombings