View source: R/civic_data_utils.R
| civic_split | R Documentation |
Splits a data frame into training and test sets. The seed is always stored in the returned object so the split is fully reproducible. Optional stratification preserves class proportions.
civic_split(data, prop = 0.75, seed = 2025L, stratify = NULL)
data |
A 'data.frame' or 'tibble'. |
prop |
Proportion for training (default '0.75'). |
seed |
Integer random seed (default '2025'). |
stratify |
Optional column name (character) to stratify on. Ensures class proportions are preserved in both splits. Works for both factor (classification) and numeric targets (stratifies by quartile). |
A named list with elements 'train', 'test', 'seed', and 'prop'.
# Any data frame works
splits <- civic_split(iris, prop = 0.8, stratify = "Species")
nrow(splits$train) # ~120
nrow(splits$test) # ~30
# Numeric stratification (by quartile)
splits2 <- civic_split(mtcars, prop = 0.75, stratify = "mpg")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.