Select a number or proportion of rows randomly from the data frame

sample_dt is a merged version of sample_n_dt and sample_frac_dt, this could be convenient.

sample_dt(.data, n = NULL, prop = NULL, replace = FALSE, by = NULL)

sample_n_dt(.data, size, replace = FALSE, by = NULL)

sample_frac_dt(.data, size, replace = FALSE, by = NULL)

Arguments

.data

A data.frame

n

Number of rows to select

prop

Fraction of rows to select

replace

Sample with or without replacement? Default uses FALSE.

by

(Optional) Character. Specify if you want to sample by group.

size

For sample_n_dt, the number of rows to select. For sample_frac_dt, the fraction of rows to select.

Value

data.table

Examples

sample_n_dt(mtcars, 10)
#>       mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>     <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num>
#>  1:  15.8     8 351.0   264  4.22 3.170 14.50     0     1     5     4
#>  2:  19.7     6 145.0   175  3.62 2.770 15.50     0     1     5     6
#>  3:  21.4     6 258.0   110  3.08 3.215 19.44     1     0     3     1
#>  4:  14.3     8 360.0   245  3.21 3.570 15.84     0     0     3     4
#>  5:  10.4     8 472.0   205  2.93 5.250 17.98     0     0     3     4
#>  6:  17.3     8 275.8   180  3.07 3.730 17.60     0     0     3     3
#>  7:  15.5     8 318.0   150  2.76 3.520 16.87     0     0     3     2
#>  8:  21.0     6 160.0   110  3.90 2.875 17.02     0     1     4     4
#>  9:  13.3     8 350.0   245  3.73 3.840 15.41     0     0     3     4
#> 10:  27.3     4  79.0    66  4.08 1.935 18.90     1     1     4     1
sample_n_dt(mtcars, 50, replace = TRUE)
#>       mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>     <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num>
#>  1:  24.4     4 146.7    62  3.69 3.190 20.00     1     0     4     2
#>  2:  14.7     8 440.0   230  3.23 5.345 17.42     0     0     3     4
#>  3:  19.2     6 167.6   123  3.92 3.440 18.30     1     0     4     4
#>  4:  16.4     8 275.8   180  3.07 4.070 17.40     0     0     3     3
#>  5:  15.0     8 301.0   335  3.54 3.570 14.60     0     1     5     8
#>  6:  10.4     8 472.0   205  2.93 5.250 17.98     0     0     3     4
#>  7:  15.2     8 275.8   180  3.07 3.780 18.00     0     0     3     3
#>  8:  19.2     8 400.0   175  3.08 3.845 17.05     0     0     3     2
#>  9:  21.0     6 160.0   110  3.90 2.620 16.46     0     1     4     4
#> 10:  15.2     8 304.0   150  3.15 3.435 17.30     0     0     3     2
#> 11:  17.3     8 275.8   180  3.07 3.730 17.60     0     0     3     3
#> 12:  16.4     8 275.8   180  3.07 4.070 17.40     0     0     3     3
#> 13:  22.8     4 108.0    93  3.85 2.320 18.61     1     1     4     1
#> 14:  14.3     8 360.0   245  3.21 3.570 15.84     0     0     3     4
#> 15:  17.8     6 167.6   123  3.92 3.440 18.90     1     0     4     4
#> 16:  26.0     4 120.3    91  4.43 2.140 16.70     0     1     5     2
#> 17:  24.4     4 146.7    62  3.69 3.190 20.00     1     0     4     2
#> 18:  13.3     8 350.0   245  3.73 3.840 15.41     0     0     3     4
#> 19:  26.0     4 120.3    91  4.43 2.140 16.70     0     1     5     2
#> 20:  24.4     4 146.7    62  3.69 3.190 20.00     1     0     4     2
#> 21:  22.8     4 140.8    95  3.92 3.150 22.90     1     0     4     2
#> 22:  33.9     4  71.1    65  4.22 1.835 19.90     1     1     4     1
#> 23:  18.1     6 225.0   105  2.76 3.460 20.22     1     0     3     1
#> 24:  30.4     4  95.1   113  3.77 1.513 16.90     1     1     5     2
#> 25:  19.7     6 145.0   175  3.62 2.770 15.50     0     1     5     6
#> 26:  21.5     4 120.1    97  3.70 2.465 20.01     1     0     3     1
#> 27:  27.3     4  79.0    66  4.08 1.935 18.90     1     1     4     1
#> 28:  10.4     8 460.0   215  3.00 5.424 17.82     0     0     3     4
#> 29:  27.3     4  79.0    66  4.08 1.935 18.90     1     1     4     1
#> 30:  14.7     8 440.0   230  3.23 5.345 17.42     0     0     3     4
#> 31:  17.3     8 275.8   180  3.07 3.730 17.60     0     0     3     3
#> 32:  32.4     4  78.7    66  4.08 2.200 19.47     1     1     4     1
#> 33:  15.0     8 301.0   335  3.54 3.570 14.60     0     1     5     8
#> 34:  19.2     6 167.6   123  3.92 3.440 18.30     1     0     4     4
#> 35:  15.8     8 351.0   264  4.22 3.170 14.50     0     1     5     4
#> 36:  22.8     4 108.0    93  3.85 2.320 18.61     1     1     4     1
#> 37:  18.7     8 360.0   175  3.15 3.440 17.02     0     0     3     2
#> 38:  15.2     8 304.0   150  3.15 3.435 17.30     0     0     3     2
#> 39:  15.2     8 304.0   150  3.15 3.435 17.30     0     0     3     2
#> 40:  18.1     6 225.0   105  2.76 3.460 20.22     1     0     3     1
#> 41:  17.3     8 275.8   180  3.07 3.730 17.60     0     0     3     3
#> 42:  21.4     4 121.0   109  4.11 2.780 18.60     1     1     4     2
#> 43:  19.7     6 145.0   175  3.62 2.770 15.50     0     1     5     6
#> 44:  15.5     8 318.0   150  2.76 3.520 16.87     0     0     3     2
#> 45:  15.5     8 318.0   150  2.76 3.520 16.87     0     0     3     2
#> 46:  21.0     6 160.0   110  3.90 2.875 17.02     0     1     4     4
#> 47:  15.2     8 304.0   150  3.15 3.435 17.30     0     0     3     2
#> 48:  21.4     4 121.0   109  4.11 2.780 18.60     1     1     4     2
#> 49:  18.7     8 360.0   175  3.15 3.440 17.02     0     0     3     2
#> 50:  15.5     8 318.0   150  2.76 3.520 16.87     0     0     3     2
#>       mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
sample_frac_dt(mtcars, 0.1)
#>      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>    <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num>
#> 1:  26.0     4 120.3    91  4.43  2.14 16.70     0     1     5     2
#> 2:  18.7     8 360.0   175  3.15  3.44 17.02     0     0     3     2
#> 3:  16.4     8 275.8   180  3.07  4.07 17.40     0     0     3     3
sample_frac_dt(mtcars, 1.5, replace = TRUE)
#>       mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>     <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num>
#>  1:  33.9     4  71.1    65  4.22 1.835 19.90     1     1     4     1
#>  2:  27.3     4  79.0    66  4.08 1.935 18.90     1     1     4     1
#>  3:  21.0     6 160.0   110  3.90 2.620 16.46     0     1     4     4
#>  4:  17.3     8 275.8   180  3.07 3.730 17.60     0     0     3     3
#>  5:  19.2     6 167.6   123  3.92 3.440 18.30     1     0     4     4
#>  6:  21.0     6 160.0   110  3.90 2.875 17.02     0     1     4     4
#>  7:  30.4     4  75.7    52  4.93 1.615 18.52     1     1     4     2
#>  8:  19.7     6 145.0   175  3.62 2.770 15.50     0     1     5     6
#>  9:  24.4     4 146.7    62  3.69 3.190 20.00     1     0     4     2
#> 10:  19.2     6 167.6   123  3.92 3.440 18.30     1     0     4     4
#> 11:  15.8     8 351.0   264  4.22 3.170 14.50     0     1     5     4
#> 12:  19.2     6 167.6   123  3.92 3.440 18.30     1     0     4     4
#> 13:  21.4     4 121.0   109  4.11 2.780 18.60     1     1     4     2
#> 14:  19.2     8 400.0   175  3.08 3.845 17.05     0     0     3     2
#> 15:  10.4     8 472.0   205  2.93 5.250 17.98     0     0     3     4
#> 16:  22.8     4 108.0    93  3.85 2.320 18.61     1     1     4     1
#> 17:  21.4     4 121.0   109  4.11 2.780 18.60     1     1     4     2
#> 18:  19.7     6 145.0   175  3.62 2.770 15.50     0     1     5     6
#> 19:  18.7     8 360.0   175  3.15 3.440 17.02     0     0     3     2
#> 20:  21.4     6 258.0   110  3.08 3.215 19.44     1     0     3     1
#> 21:  15.2     8 304.0   150  3.15 3.435 17.30     0     0     3     2
#> 22:  32.4     4  78.7    66  4.08 2.200 19.47     1     1     4     1
#> 23:  21.0     6 160.0   110  3.90 2.620 16.46     0     1     4     4
#> 24:  18.1     6 225.0   105  2.76 3.460 20.22     1     0     3     1
#> 25:  27.3     4  79.0    66  4.08 1.935 18.90     1     1     4     1
#> 26:  18.7     8 360.0   175  3.15 3.440 17.02     0     0     3     2
#> 27:  10.4     8 460.0   215  3.00 5.424 17.82     0     0     3     4
#> 28:  21.0     6 160.0   110  3.90 2.620 16.46     0     1     4     4
#> 29:  33.9     4  71.1    65  4.22 1.835 19.90     1     1     4     1
#> 30:  32.4     4  78.7    66  4.08 2.200 19.47     1     1     4     1
#> 31:  10.4     8 472.0   205  2.93 5.250 17.98     0     0     3     4
#> 32:  18.1     6 225.0   105  2.76 3.460 20.22     1     0     3     1
#> 33:  15.5     8 318.0   150  2.76 3.520 16.87     0     0     3     2
#> 34:  14.7     8 440.0   230  3.23 5.345 17.42     0     0     3     4
#> 35:  16.4     8 275.8   180  3.07 4.070 17.40     0     0     3     3
#> 36:  15.2     8 304.0   150  3.15 3.435 17.30     0     0     3     2
#> 37:  18.1     6 225.0   105  2.76 3.460 20.22     1     0     3     1
#> 38:  19.7     6 145.0   175  3.62 2.770 15.50     0     1     5     6
#> 39:  19.2     6 167.6   123  3.92 3.440 18.30     1     0     4     4
#> 40:  15.8     8 351.0   264  4.22 3.170 14.50     0     1     5     4
#> 41:  19.2     8 400.0   175  3.08 3.845 17.05     0     0     3     2
#> 42:  21.5     4 120.1    97  3.70 2.465 20.01     1     0     3     1
#> 43:  17.3     8 275.8   180  3.07 3.730 17.60     0     0     3     3
#> 44:  18.7     8 360.0   175  3.15 3.440 17.02     0     0     3     2
#> 45:  19.2     6 167.6   123  3.92 3.440 18.30     1     0     4     4
#> 46:  27.3     4  79.0    66  4.08 1.935 18.90     1     1     4     1
#> 47:  30.4     4  95.1   113  3.77 1.513 16.90     1     1     5     2
#> 48:  15.5     8 318.0   150  2.76 3.520 16.87     0     0     3     2
#>       mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb


sample_dt(mtcars,n=10)
#>       mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>     <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num>
#>  1:  18.1     6 225.0   105  2.76 3.460 20.22     1     0     3     1
#>  2:  32.4     4  78.7    66  4.08 2.200 19.47     1     1     4     1
#>  3:  18.7     8 360.0   175  3.15 3.440 17.02     0     0     3     2
#>  4:  17.8     6 167.6   123  3.92 3.440 18.90     1     0     4     4
#>  5:  21.4     6 258.0   110  3.08 3.215 19.44     1     0     3     1
#>  6:  15.8     8 351.0   264  4.22 3.170 14.50     0     1     5     4
#>  7:  22.8     4 108.0    93  3.85 2.320 18.61     1     1     4     1
#>  8:  26.0     4 120.3    91  4.43 2.140 16.70     0     1     5     2
#>  9:  22.8     4 140.8    95  3.92 3.150 22.90     1     0     4     2
#> 10:  30.4     4  95.1   113  3.77 1.513 16.90     1     1     5     2
sample_dt(mtcars,prop = 0.1)
#>      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>    <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num>
#> 1:  19.2     6 167.6   123  3.92 3.440 18.30     1     0     4     4
#> 2:  13.3     8 350.0   245  3.73 3.840 15.41     0     0     3     4
#> 3:  21.5     4 120.1    97  3.70 2.465 20.01     1     0     3     1


# sample by group(s)
iris %>% sample_n_dt(2,by = "Species")
#>       Species Sepal.Length Sepal.Width Petal.Length Petal.Width
#>        <fctr>        <num>       <num>        <num>       <num>
#> 1:     setosa          5.5         3.5          1.3         0.2
#> 2:     setosa          5.5         4.2          1.4         0.2
#> 3: versicolor          6.0         2.2          4.0         1.0
#> 4: versicolor          5.4         3.0          4.5         1.5
#> 5:  virginica          6.7         3.1          5.6         2.4
#> 6:  virginica          7.3         2.9          6.3         1.8
iris %>% sample_frac_dt(.1,by = "Species")
#>        Species Sepal.Length Sepal.Width Petal.Length Petal.Width
#>         <fctr>        <num>       <num>        <num>       <num>
#>  1:     setosa          5.5         4.2          1.4         0.2
#>  2:     setosa          5.0         3.0          1.6         0.2
#>  3:     setosa          5.0         3.3          1.4         0.2
#>  4:     setosa          5.0         3.2          1.2         0.2
#>  5:     setosa          4.9         3.1          1.5         0.1
#>  6:     setosa          5.1         3.8          1.5         0.3
#>  7:     setosa          5.0         3.4          1.5         0.2
#>  8:     setosa          5.4         3.4          1.7         0.2
#>  9:     setosa          5.2         3.5          1.5         0.2
#> 10:     setosa          5.1         3.8          1.6         0.2
#> 11:     setosa          5.7         3.8          1.7         0.3
#> 12:     setosa          5.0         3.5          1.6         0.6
#> 13:     setosa          4.6         3.1          1.5         0.2
#> 14:     setosa          5.0         3.6          1.4         0.2
#> 15:     setosa          5.1         3.4          1.5         0.2
#> 16: versicolor          5.5         2.6          4.4         1.2
#> 17: versicolor          6.4         2.9          4.3         1.3
#> 18: versicolor          5.6         2.9          3.6         1.3
#> 19: versicolor          6.9         3.1          4.9         1.5
#> 20: versicolor          6.4         3.2          4.5         1.5
#> 21: versicolor          6.2         2.9          4.3         1.3
#> 22: versicolor          5.6         2.5          3.9         1.1
#> 23: versicolor          6.7         3.1          4.4         1.4
#> 24: versicolor          6.5         2.8          4.6         1.5
#> 25: versicolor          5.7         2.6          3.5         1.0
#> 26: versicolor          5.1         2.5          3.0         1.1
#> 27: versicolor          5.0         2.3          3.3         1.0
#> 28: versicolor          6.0         2.2          4.0         1.0
#> 29: versicolor          6.1         2.8          4.0         1.3
#> 30: versicolor          6.7         3.1          4.7         1.5
#> 31:  virginica          6.4         3.1          5.5         1.8
#> 32:  virginica          6.3         3.4          5.6         2.4
#> 33:  virginica          7.9         3.8          6.4         2.0
#> 34:  virginica          5.6         2.8          4.9         2.0
#> 35:  virginica          5.7         2.5          5.0         2.0
#> 36:  virginica          7.2         3.6          6.1         2.5
#> 37:  virginica          6.9         3.2          5.7         2.3
#> 38:  virginica          6.4         2.8          5.6         2.2
#> 39:  virginica          6.2         3.4          5.4         2.3
#> 40:  virginica          6.8         3.2          5.9         2.3
#> 41:  virginica          7.3         2.9          6.3         1.8
#> 42:  virginica          4.9         2.5          4.5         1.7
#> 43:  virginica          6.5         3.2          5.1         2.0
#> 44:  virginica          6.5         3.0          5.8         2.2
#> 45:  virginica          7.2         3.0          5.8         1.6
#>        Species Sepal.Length Sepal.Width Petal.Length Petal.Width

mtcars %>% sample_n_dt(1,by = c("cyl","vs"))
#>      cyl    vs   mpg  disp    hp  drat    wt  qsec    am  gear  carb
#>    <num> <num> <num> <num> <num> <num> <num> <num> <num> <num> <num>
#> 1:     6     0  21.0 160.0   110  3.90  2.62 16.46     1     4     4
#> 2:     4     1  24.4 146.7    62  3.69  3.19 20.00     0     4     2
#> 3:     6     1  18.1 225.0   105  2.76  3.46 20.22     0     3     1
#> 4:     8     0  15.2 275.8   180  3.07  3.78 18.00     0     3     3
#> 5:     4     0  26.0 120.3    91  4.43  2.14 16.70     1     5     2