A data frame is split according to some variables in a formula, and a sample of a certain fraction of each is drawn.

sample_by(data, formula, frac = 0.1, replace = FALSE, systematic = FALSE)

sampleBy(
  formula,
  frac = 0.1,
  replace = FALSE,
  data = parent.frame(),
  systematic = FALSE
)

Arguments

data

A data frame.

formula

A formula defining the grouping of the data frame.

frac

The part of data to be sampled.

replace

Is the sampling with replacement.

systematic

Should sampling be systematic.

Value

A dataframe.

Details

If systematic=FALSE (default) then frac gives the fraction of data sampled. If systematic=TRUE and frac=.2 then every 1/.2 i.e. every 5th observation is taken out.

Examples

data(dietox)
sampleBy(formula = ~ Evit + Cu, frac=.1, data = dietox)
#>                    Pig    Evit    Cu Litter    Start    Weight       Feed Time
#> Evit000|Cu000.1   4601 Evit000 Cu000      1 26.50000  26.50000         NA    1
#> Evit000|Cu000.97  4757 Evit000 Cu000      3 26.89999  26.89999         NA    1
#> Evit000|Cu000.106 4757 Evit000 Cu000      3 26.89999  81.00000 129.000000   10
#> Evit000|Cu000.193 4856 Evit000 Cu000      6 32.00000  32.00000         NA    1
#> Evit000|Cu000.202 4856 Evit000 Cu000      6 32.00000  92.50000 131.000000   10
#> Evit000|Cu000.255 5497 Evit000 Cu000      8 21.20000  29.39999  16.500000    3
#> Evit000|Cu000.398 5852 Evit000 Cu000     12 23.79999  50.39999  44.900002    5
#> Evit000|Cu000.403 5852 Evit000 Cu000     12 23.79999  91.19995 132.000000   10
#> Evit000|Cu035.14  4602 Evit000 Cu035      1 28.29999  30.09999   3.300003    2
#> Evit000|Cu035.20  4602 Evit000 Cu035      1 28.29999  73.00000  90.800003    8
#> Evit000|Cu035.268 5500 Evit000 Cu035      8 17.00000  29.39999  32.800003    4
#> Evit000|Cu035.273 5500 Evit000 Cu035      8 17.00000  62.50000 103.800003    9
#> Evit000|Cu035.275 5500 Evit000 Cu035      8 17.00000  78.29999 147.300001   11
#> Evit000|Cu035.417 5862 Evit000 Cu035     11 22.00000  93.19995 147.700005   12
#> Evit000|Cu035.708 8142 Evit000 Cu035     23 25.20000  35.69998  21.000000    3
#> Evit000|Cu035.716 8142 Evit000 Cu035     23 25.20000  89.39996 138.500000   11
#> Evit000|Cu035.816 8273 Evit000 Cu035     22 25.00000  37.09998  20.000000    3
#> Evit000|Cu035.821 8273 Evit000 Cu035     22 25.00000  78.19995 102.000000    8
#> Evit200|Cu175.26  4603 Evit200 Cu175      1 27.59999  30.59999   6.700005    2
#> Evit200|Cu175.27  4603 Evit200 Cu175      1 27.59999  38.69998  17.600000    3
#> Evit200|Cu175.30  4603 Evit200 Cu175      1 27.59999  61.50000  64.600002    6
#> Evit200|Cu175.235 5389 Evit200 Cu175      7 24.29999  58.09998  69.000002    7
#> Evit200|Cu175.238 5389 Evit200 Cu175      7 24.29999  78.39996 114.200003   10
#> Evit200|Cu175.383 5851 Evit200 Cu175     12 22.29999  24.70000   5.900002    2
#> Evit200|Cu175.385 5851 Evit200 Cu175     12 22.29999  36.89999  25.200001    4
#> Evit200|Cu175.673 8053 Evit200 Cu175     20 21.89999  41.29999  36.700001    4
#> Evit200|Cu175.674 8053 Evit200 Cu175     20 21.89999  48.69998  51.800003    5
#> Evit200|Cu175.677 8053 Evit200 Cu175     20 21.89999  71.29999 115.800003    8
#> Evit100|Cu035.48  4605 Evit100 Cu035      1 31.50000 109.19995 176.400002   12
#> Evit100|Cu035.218 4858 Evit100 Cu035      6 28.00000  31.20000   8.000000    2
#> Evit100|Cu035.220 4858 Evit100 Cu035      6 28.00000  44.69998  31.800003    4
#> Evit100|Cu035.578 6432 Evit100 Cu035      5 25.09999  44.19998  31.000000    5
#> Evit100|Cu035.579 6432 Evit100 Cu035      5 25.09999  52.79999  56.300003    6
#> Evit100|Cu035.663 8051 Evit100 Cu035     20 30.59999  62.39999  65.000000    6
#> Evit100|Cu035.669 8051 Evit100 Cu035     20 30.59999 112.50000 196.900002   12
#> Evit100|Cu035.722 8144 Evit100 Cu035     23 23.50000  29.79999  29.500000    5
#> Evit100|Cu035.723 8144 Evit100 Cu035     23 23.50000  32.79999  34.000000    6
#> Evit100|Cu035.728 8144 Evit100 Cu035     23 23.50000  72.09998 111.200005   11
#> Evit100|Cu175.53  4641 Evit100 Cu175      2 27.09999  56.50000  53.300003    5
#> Evit100|Cu175.157 4815 Evit100 Cu175      4 24.89999  24.89999         NA    1
#> Evit100|Cu175.158 4815 Evit100 Cu175      4 24.89999  29.70000   9.500000    2
#> Evit100|Cu175.161 4815 Evit100 Cu175      4 24.89999  48.29999  45.500000    5
#> Evit100|Cu175.284 5501 Evit100 Cu175      8 30.00000  77.39996  92.800003    8
#> Evit100|Cu175.285 5501 Evit100 Cu175      8 30.00000  86.00000 110.000000    9
#> Evit100|Cu175.439 5866 Evit100 Cu175     11 22.00000  75.00000 109.900002   10
#> Evit100|Cu175.470 6057 Evit100 Cu175     16 27.00000  53.19998  44.800003    5
#> Evit100|Cu175.471 6057 Evit100 Cu175     16 27.00000  62.39999  60.000000    6
#> Evit100|Cu175.476 6057 Evit100 Cu175     16 27.00000 103.00000 147.800003   11
#> Evit200|Cu035.79  4645 Evit200 Cu035      2 27.70000  69.79999  88.000000    7
#> Evit200|Cu035.83  4645 Evit200 Cu035      2 27.70000  96.79999 157.200001   11
#> Evit200|Cu035.119 4759 Evit200 Cu035      3 22.59999  89.39996 152.100002   11
#> Evit200|Cu035.249 5392 Evit200 Cu035      7 24.59999  63.00000  89.499996    9
#> Evit200|Cu035.250 5392 Evit200 Cu035      7 24.59999  75.69995 104.999996   10
#> Evit200|Cu035.252 5392 Evit200 Cu035      7 24.59999  88.69995 142.799997   12
#> Evit200|Cu035.444 6055 Evit200 Cu035     16 26.20000  37.39999  18.200001    3
#> Evit200|Cu035.448 6055 Evit200 Cu035     16 26.20000  64.19995  87.200001    7
#> Evit200|Cu035.561 6288 Evit200 Cu035     18 24.09999 101.59998 161.800003   12
#> Evit200|Cu035.598 6909 Evit200 Cu035     13 24.70000  24.70000         NA    1
#> Evit200|Cu035.697 8141 Evit200 Cu035     23 24.20000  35.59998  24.600002    4
#> Evit200|Cu000.88  4756 Evit200 Cu000      3 17.00000  30.00000  28.900002    4
#> Evit200|Cu000.299 5502 Evit200 Cu000      8 29.59999  96.39996 145.200001   11
#> Evit200|Cu000.323 5528 Evit200 Cu000      9 24.00000  24.00000         NA    1
#> Evit200|Cu000.333 5528 Evit200 Cu000      9 24.00000  92.50000 146.100000   11
#> Evit200|Cu000.348 5581 Evit200 Cu000     10 27.00000  37.79999  19.500000    3
#> Evit200|Cu000.353 5581 Evit200 Cu000     10 27.00000  70.50000  97.000000    8
#> Evit200|Cu000.355 5581 Evit200 Cu000     10 27.00000  92.00000 138.800003   10
#> Evit200|Cu000.375 5850 Evit200 Cu000     12 22.79999  48.19998  48.000000    6
#> Evit200|Cu000.381 5850 Evit200 Cu000     12 22.79999  94.00000 146.800001   12
#> Evit200|Cu000.632 6912 Evit200 Cu000     13 24.50000  98.89996 143.000000   11
#> Evit000|Cu175.169 4817 Evit000 Cu175      4 24.59999  24.59999         NA    1
#> Evit000|Cu175.173 4817 Evit000 Cu175      4 24.59999  49.69998  47.000004    5
#> Evit000|Cu175.175 4817 Evit000 Cu175      4 24.59999  66.89996  83.300003    7
#> Evit000|Cu175.214 4857 Evit000 Cu175      6 26.00000  86.00000 135.300007   10
#> Evit000|Cu175.650 8050 Evit000 Cu175     20 26.79999  56.19998  54.400002    5
#> Evit000|Cu175.657 8050 Evit000 Cu175     20 26.79999 112.00000 224.000000   12
#> Evit000|Cu175.684 8139 Evit000 Cu175     23 24.00000  33.00000  20.000000    3
#> Evit000|Cu175.791 8270 Evit000 Cu175     22 23.70000  28.79999   7.600000    2
#> Evit000|Cu175.793 8270 Evit000 Cu175     22 23.70000  43.00000  33.200001    4
#> Evit000|Cu175.799 8270 Evit000 Cu175     22 23.70000  90.69995 133.200001   10
#> Evit100|Cu000.183 4854 Evit100 Cu000      6 29.00000  38.29999  19.800003    3
#> Evit100|Cu000.187 4854 Evit100 Cu000      6 29.00000  62.59998  70.000000    7
#> Evit100|Cu000.191 4854 Evit100 Cu000      6 29.00000  85.50000 138.500000   11
#> Evit100|Cu000.192 4854 Evit100 Cu000      6 29.00000  93.09998 158.000000   12
#> Evit100|Cu000.517 6211 Evit100 Cu000     17 24.50000  46.39999  30.100006    4
#> Evit100|Cu000.527 6284 Evit100 Cu000     18 23.09999  26.00000   5.900002    2
#> Evit100|Cu000.591 6433 Evit100 Cu000      5 32.19998  60.09998  58.600000    6
#> Evit100|Cu000.596 6433 Evit100 Cu000      5 32.19998 111.09998 163.000000   11
#> Evit100|Cu000.803 8271 Evit100 Cu000     22 24.00000  28.89999   8.500000    2
#> Evit100|Cu000.813 8271 Evit100 Cu000     22 24.00000  99.79999 158.100000   12