Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is <em>way</em> more than what you asked for, but I wrote a function called <a href="https://gist.github.com/mrdwab/6424112" rel="nofollow"><code>stratified</code></a> that lets you take random samples from a <code>data.frame</code> by one or more group variables.</p> <p>You can load it and use it like this:</p> <pre><code>library(devtools) source_gist("https://gist.github.com/mrdwab/6424112") # [1] "https://raw.github.com/gist/6424112" # SHA-1 hash of file is 0006d8548785ec8a5651c3dd599648cc88d153a4 ## One row stratified(mydf, "v2", 1) # v1 v2 # 10 E 1 # 8 C 2 ## Two rows stratified(mydf, "v2", 2) # v1 v2 # 2 B 1 # 6 B 1 # 3 C 2 # 5 B 2 </code></pre> <hr> <p>I'll add official documentation to the function at some point, but here's a summary to help you get the best use out of it:</p> <p>The arguments to <code>stratified</code> are:</p> <ul> <li><code>df</code>: The input <code>data.frame</code></li> <li><code>group</code>: A character vector of the column or columns that make up the "strata".</li> <li><code>size</code>: The desired sample size. <ul> <li>If <code>size</code> is a value less than 1, a proportionate sample is taken from each stratum.</li> <li>If <code>size</code> is a single integer of 1 or more, that number of samples is taken from each stratum.</li> <li>If <code>size</code> is a vector of integers, the specified number of samples is taken for each stratum. It is recommended that you use a <em>named vector</em>. For example, if you have two strata, "A" and "B", and you wanted 5 samples from "A" and 10 from "B", you would enter <code>size = c(A = 5, B = 10)</code>.</li> </ul></li> <li><code>select</code>: This allows you to subset the groups in the sampling process. This is a <code>list</code>. For instance, if your <code>group</code> variable was "Group", and it contained three strata, "A", "B", and "C", but you only wanted to sample from "A" and "C", you can use <code>select = list(Group = c("A", "C"))</code>.</li> <li><code>replace</code>: For sampling with replacement.</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload