How to get random rows with special condition in R The 2019 Stack Overflow Developer Survey Results Are InHow to sort a dataframe by multiple column(s)Filter data.frame rows by a logical conditionGrouping functions (tapply, by, aggregate) and the *apply familyHow to make a great R reproducible exampleHow to unload a package without restarting RFind the day of a weekAggregate / summarize multiple variables per group (e.g. sum, mean)Return data subset time frames within another timeframes?Extracting a random sample of rows in a data.frame with a nested conditionaldata.table vs dplyr: can one do something well the other can't or does poorly?

Is "plugging out" electronic devices an American expression?

Are USB sockets on wall outlets live all the time, even when the switch is off?

Carnot-Caratheodory metric

Falsification in Math vs Science

How to deal with fear of taking dependencies

Why isn't airport relocation done gradually?

Patience, young "Padovan"

Lethal sonic weapons

Why is the maximum length of OpenWrt’s root password 8 characters?

Limit to 0 ambiguity

Could a US political party gain complete control over the government by removing checks & balances?

Why can Shazam do this?

What does Linus Torvalds mean when he says that Git "never ever" tracks a file?

Landlord wants to switch my lease to a "Land contract" to "get back at the city"

Protecting Dualbooting Windows from dangerous code (like rm -rf)

Should I write numbers in words or as numerals when there are multiple next to each other?

Is domain driven design an anti-SQL pattern?

What do hard-Brexiteers want with respect to the Irish border?

Inflated grade on resume at previous job, might former employer tell new employer?

How long do I have to send my income tax payment to the IRS?

Where does the "burst of radiance" from Holy Weapon originate?

Pristine Bit Checking

Why don't Unix/Linux systems traverse through directories until they find the required version of a linked library?

What is the steepest angle that a canal can be traversable without locks?



How to get random rows with special condition in R



The 2019 Stack Overflow Developer Survey Results Are InHow to sort a dataframe by multiple column(s)Filter data.frame rows by a logical conditionGrouping functions (tapply, by, aggregate) and the *apply familyHow to make a great R reproducible exampleHow to unload a package without restarting RFind the day of a weekAggregate / summarize multiple variables per group (e.g. sum, mean)Return data subset time frames within another timeframes?Extracting a random sample of rows in a data.frame with a nested conditionaldata.table vs dplyr: can one do something well the other can't or does poorly?



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








-2















Hello how can I get 2 random rows with special condition. The example below are dates and I want to get a random sample of 2 dates with any year but month and day is 12/31. So it's like "XXXX1231".



> dateDS
dateDS
1 20121231
2 20131020
3 20140819
4 20151231
5 20161231
6 20171106
7 20131231


And I want to get a random sample that would output 2 dates ("XXXX1231")



> dateSample
dateSample
1 20121231
2 20131231









share|improve this question




























    -2















    Hello how can I get 2 random rows with special condition. The example below are dates and I want to get a random sample of 2 dates with any year but month and day is 12/31. So it's like "XXXX1231".



    > dateDS
    dateDS
    1 20121231
    2 20131020
    3 20140819
    4 20151231
    5 20161231
    6 20171106
    7 20131231


    And I want to get a random sample that would output 2 dates ("XXXX1231")



    > dateSample
    dateSample
    1 20121231
    2 20131231









    share|improve this question
























      -2












      -2








      -2








      Hello how can I get 2 random rows with special condition. The example below are dates and I want to get a random sample of 2 dates with any year but month and day is 12/31. So it's like "XXXX1231".



      > dateDS
      dateDS
      1 20121231
      2 20131020
      3 20140819
      4 20151231
      5 20161231
      6 20171106
      7 20131231


      And I want to get a random sample that would output 2 dates ("XXXX1231")



      > dateSample
      dateSample
      1 20121231
      2 20131231









      share|improve this question














      Hello how can I get 2 random rows with special condition. The example below are dates and I want to get a random sample of 2 dates with any year but month and day is 12/31. So it's like "XXXX1231".



      > dateDS
      dateDS
      1 20121231
      2 20131020
      3 20140819
      4 20151231
      5 20161231
      6 20171106
      7 20131231


      And I want to get a random sample that would output 2 dates ("XXXX1231")



      > dateSample
      dateSample
      1 20121231
      2 20131231






      r






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 8 at 8:43









      Mr. BusterMr. Buster

      484




      484






















          2 Answers
          2






          active

          oldest

          votes


















          4














          One way using grep find indices of dates which ends with "1231" and then subset them.



          df[sample(grep("1231$", df$dateDS), 2), ,drop = FALSE]

          # dateDS
          #5 20161231
          #4 20151231


          Or if you want just values of those dates and don't want to subset



          sample(grep("1231$", df$dateDS, value = TRUE), 2)
          #[1] "20151231" "20161231"



          Another option, convert the dateDS to actual date extract month and date and randomly select two values with "1231" in it.



          df$dateDS[sample(which(format(as.Date(as.character(df$dateDS), "%Y%m%d"), "%m%d") == "1231"), 2)]





          share|improve this answer






























            1














            Another option with substr() in order to select only dates with 1231, then sample two rows:



            d <- read.table(text="dateDS
            20121231
            20131020
            20140819
            20151231
            20161231
            20171106
            20131231", header=T)

            d$md <- substr(d$dateDS, 5, 8) # use md to select only "1231"
            d <- d[d$md==1231, ]
            # d ateDS md
            # 1 20121231 1231
            # 4 20151231 1231
            # 5 20161231 1231
            # 7 20131231 1231
            d[sample(2), -2]
            #[1] 20151231 20121231





            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%2f55059533%2fhow-to-get-random-rows-with-special-condition-in-r%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









              4














              One way using grep find indices of dates which ends with "1231" and then subset them.



              df[sample(grep("1231$", df$dateDS), 2), ,drop = FALSE]

              # dateDS
              #5 20161231
              #4 20151231


              Or if you want just values of those dates and don't want to subset



              sample(grep("1231$", df$dateDS, value = TRUE), 2)
              #[1] "20151231" "20161231"



              Another option, convert the dateDS to actual date extract month and date and randomly select two values with "1231" in it.



              df$dateDS[sample(which(format(as.Date(as.character(df$dateDS), "%Y%m%d"), "%m%d") == "1231"), 2)]





              share|improve this answer



























                4














                One way using grep find indices of dates which ends with "1231" and then subset them.



                df[sample(grep("1231$", df$dateDS), 2), ,drop = FALSE]

                # dateDS
                #5 20161231
                #4 20151231


                Or if you want just values of those dates and don't want to subset



                sample(grep("1231$", df$dateDS, value = TRUE), 2)
                #[1] "20151231" "20161231"



                Another option, convert the dateDS to actual date extract month and date and randomly select two values with "1231" in it.



                df$dateDS[sample(which(format(as.Date(as.character(df$dateDS), "%Y%m%d"), "%m%d") == "1231"), 2)]





                share|improve this answer

























                  4












                  4








                  4







                  One way using grep find indices of dates which ends with "1231" and then subset them.



                  df[sample(grep("1231$", df$dateDS), 2), ,drop = FALSE]

                  # dateDS
                  #5 20161231
                  #4 20151231


                  Or if you want just values of those dates and don't want to subset



                  sample(grep("1231$", df$dateDS, value = TRUE), 2)
                  #[1] "20151231" "20161231"



                  Another option, convert the dateDS to actual date extract month and date and randomly select two values with "1231" in it.



                  df$dateDS[sample(which(format(as.Date(as.character(df$dateDS), "%Y%m%d"), "%m%d") == "1231"), 2)]





                  share|improve this answer













                  One way using grep find indices of dates which ends with "1231" and then subset them.



                  df[sample(grep("1231$", df$dateDS), 2), ,drop = FALSE]

                  # dateDS
                  #5 20161231
                  #4 20151231


                  Or if you want just values of those dates and don't want to subset



                  sample(grep("1231$", df$dateDS, value = TRUE), 2)
                  #[1] "20151231" "20161231"



                  Another option, convert the dateDS to actual date extract month and date and randomly select two values with "1231" in it.



                  df$dateDS[sample(which(format(as.Date(as.character(df$dateDS), "%Y%m%d"), "%m%d") == "1231"), 2)]






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 8 at 8:47









                  Ronak ShahRonak Shah

                  45.8k104268




                  45.8k104268























                      1














                      Another option with substr() in order to select only dates with 1231, then sample two rows:



                      d <- read.table(text="dateDS
                      20121231
                      20131020
                      20140819
                      20151231
                      20161231
                      20171106
                      20131231", header=T)

                      d$md <- substr(d$dateDS, 5, 8) # use md to select only "1231"
                      d <- d[d$md==1231, ]
                      # d ateDS md
                      # 1 20121231 1231
                      # 4 20151231 1231
                      # 5 20161231 1231
                      # 7 20131231 1231
                      d[sample(2), -2]
                      #[1] 20151231 20121231





                      share|improve this answer



























                        1














                        Another option with substr() in order to select only dates with 1231, then sample two rows:



                        d <- read.table(text="dateDS
                        20121231
                        20131020
                        20140819
                        20151231
                        20161231
                        20171106
                        20131231", header=T)

                        d$md <- substr(d$dateDS, 5, 8) # use md to select only "1231"
                        d <- d[d$md==1231, ]
                        # d ateDS md
                        # 1 20121231 1231
                        # 4 20151231 1231
                        # 5 20161231 1231
                        # 7 20131231 1231
                        d[sample(2), -2]
                        #[1] 20151231 20121231





                        share|improve this answer

























                          1












                          1








                          1







                          Another option with substr() in order to select only dates with 1231, then sample two rows:



                          d <- read.table(text="dateDS
                          20121231
                          20131020
                          20140819
                          20151231
                          20161231
                          20171106
                          20131231", header=T)

                          d$md <- substr(d$dateDS, 5, 8) # use md to select only "1231"
                          d <- d[d$md==1231, ]
                          # d ateDS md
                          # 1 20121231 1231
                          # 4 20151231 1231
                          # 5 20161231 1231
                          # 7 20131231 1231
                          d[sample(2), -2]
                          #[1] 20151231 20121231





                          share|improve this answer













                          Another option with substr() in order to select only dates with 1231, then sample two rows:



                          d <- read.table(text="dateDS
                          20121231
                          20131020
                          20140819
                          20151231
                          20161231
                          20171106
                          20131231", header=T)

                          d$md <- substr(d$dateDS, 5, 8) # use md to select only "1231"
                          d <- d[d$md==1231, ]
                          # d ateDS md
                          # 1 20121231 1231
                          # 4 20151231 1231
                          # 5 20161231 1231
                          # 7 20131231 1231
                          d[sample(2), -2]
                          #[1] 20151231 20121231






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Mar 8 at 8:50









                          RLaveRLave

                          5,29911227




                          5,29911227



























                              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%2f55059533%2fhow-to-get-random-rows-with-special-condition-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

                              1928 у кіно

                              Захаров Федір Захарович

                              Ель Греко