Description Usage Arguments Examples
This is a wrapper around sample.int
to make it easy to
select random rows from a table. It currently only works for local
tbls.
1 2 3 4 | sample_n(tbl, size, replace = FALSE, weight = NULL, .env = parent.frame())
sample_frac(tbl, size = 1, replace = FALSE, weight = NULL,
.env = parent.frame())
|
tbl |
tbl of data. |
size |
For |
replace |
Sample with or without replacement? |
weight |
Sampling weights. This expression is evaluated in the context of the data frame. It must return a vector of non-negative numbers the same length as the input. Weights are automatically standardised to sum to 1. |
.env |
Environment in which to look for non-data names used in
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | by_cyl <- mtcars %>% group_by(cyl)
# Sample fixed number per group
sample_n(mtcars, 10)
sample_n(mtcars, 50, replace = TRUE)
sample_n(mtcars, 10, weight = mpg)
sample_n(by_cyl, 3)
sample_n(by_cyl, 10, replace = TRUE)
sample_n(by_cyl, 3, weight = mpg / mean(mpg))
# Sample fixed fraction per group
# Default is to sample all data = randomly resample rows
sample_frac(mtcars)
sample_frac(mtcars, 0.1)
sample_frac(mtcars, 1.5, replace = TRUE)
sample_frac(mtcars, 0.1, weight = 1 / mpg)
sample_frac(by_cyl, 0.2)
sample_frac(by_cyl, 1, replace = TRUE)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.