Sample a dataframe for n obs and returns full dataframe if n > nrow

sample_all_or_n(data, n, replace = FALSE, weight = NULL)

Arguments

data

A dataframe or tibble.

n

Number of rows to sample. If n is greater than the row count, the dataframe will be returned unchanged.

replace

Sample with or without replacement?

weight

Sampling weights. This must evaluate to a vector of non-negative numbers the same length as the input. Weights are automatically standardised to sum to 1.

See also

Examples

library(tidyverse) test_data <- tibble( Group = sample(c("Apple", "Pear"), size = 10, replace = TRUE), A = sample(c(NA_integer_, 1:3), size = 10, replace = TRUE), B = sample(c(NA_integer_, 4:6), size = 10, replace = TRUE) ) # Sample less than row count sample_all_or_n(data = test_data, n = 5)
#> # A tibble: 5 x 3 #> Group A B #> <chr> <int> <int> #> 1 Apple 1 5 #> 2 Apple 3 4 #> 3 Pear 3 4 #> 4 Apple 3 NA #> 5 Pear 1 6
# When n is greater than row count, the dataframe is returned unchanged sample_all_or_n(data = test_data, n = 11)
#> # A tibble: 10 x 3 #> Group A B #> <chr> <int> <int> #> 1 Pear 1 6 #> 2 Apple 1 5 #> 3 Apple 3 NA #> 4 Apple 3 4 #> 5 Pear 3 4 #> 6 Apple NA 4 #> 7 Apple NA 4 #> 8 Pear NA NA #> 9 Apple NA 6 #> 10 Apple 2 5