sample: Sample rows randomly from a table

Description Usage Arguments Value See Also Examples

Description

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.

Usage

1
2
3
4
5
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

See Also

sample_n,sample_frac

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
sample_n_dt(mtcars, 10)
sample_n_dt(mtcars, 50, replace = TRUE)
sample_frac_dt(mtcars, 0.1)
sample_frac_dt(mtcars, 1.5, replace = TRUE)


sample_dt(mtcars,n=10)
sample_dt(mtcars,prop = 0.1)


# sample by group(s)
iris %>% sample_n_dt(2,by = "Species")
iris %>% sample_frac_dt(.1,by = "Species")

mtcars %>% sample_n_dt(1,by = "cyl,vs")
# equals to
mtcars %>% sample_n_dt(1,by = c("cyl","vs"))

tidyfst documentation built on Sept. 1, 2020, 5:07 p.m.