How to Sample Without Replacement in R
· 3 min read · Updated March 15, 2026 · beginner
r sample random without-replacement dplyr vector
Sampling without replacement means each element can only be selected once. This is the standard approach for creating random subsets, randomized lists, and train/test splits.
The sample() Function
Base R’s sample() does this by default:
# Sample 3 numbers from 1 to 10 without replacement
sample(1:10, size = 3)
# [1] 4 9 2
# Sample 5 letters without replacement
sample(letters, size = 5)
# [1] "u" "q" "z" "k" "m"
# Shuffle all elements (randomize order)
sample(1:10)
# [1] 3 9 5 10 6 1 2 4 8 7
The default behavior is replace = FALSE, meaning no element appears twice.
With dplyr
The tidyverse approach uses slice_sample() on a vector or sample() within mutate():
library(dplyr)
# Using slice_sample on a tibble column
df <- tibble(values = 1:20)
sampled <- df %>% slice_sample(n = 5)
# Sample from a vector directly
sampled <- sample_n(tibble(x = 1:100), 10)
# A tibble: 10 × 1
# x
# <int>
# 1 23
# 2 87
# 3 12
# 4 56
# 5 91
# 6 34
# 7 8
# 8 45
# 9 67
# 10 78
Practical Examples
Create a Random Test Set
# Create indices for train/test split
set.seed(42)
n <- 100
indices <- 1:n
train_indices <- sample(indices, size = 70)
test_indices <- setdiff(indices, train_indices)
Randomize Treatment Assignment
set.seed(123)
subjects <- 1:50
# Assign 25 to treatment, 25 to control
treatment <- sample(subjects, size = 25)
control <- setdiff(subjects, treatment)
Select Random Files
# Randomly select 5 CSV files from a directory
set.seed(101)
files <- list.files("data/", pattern = "\\.csv$")
selected <- sample(files, size = min(5, length(files)))
Shuffle a Deck of Cards
# Create and shuffle a deck
suits <- c("hearts", "diamonds", "clubs", "spades")
ranks <- c("A", 2:10, "J", "Q", "K")
deck <- expand.grid(rank = ranks, suit = suits)
deck <- deck[sample(nrow(deck)), ] # Shuffle the deck
Sampling vs With-Replacement
Compare the two approaches:
set.seed(42)
# Without replacement - unique values only
sample(1:10, size = 5, replace = FALSE)
# [1] 7 1 9 6 10
# With replacement - duplicates possible
sample(1:10, size = 5, replace = TRUE)
# [1] 6 1 1 9 6
Common Variations
| Task | Code |
|---|---|
| Sample n elements | sample(x, n) |
| Shuffle all elements | sample(x) |
| Sample with probability weights | sample(x, n, prob = weights) |
| Sample from custom pool | sample(c("a", "b", "c"), 2) |
Using Probabilities
You can weight elements by probability:
set.seed(55)
# 10 elements with increasing probabilities
x <- 1:10
probs <- x / sum(x) # probability proportional to value
sample(x, size = 5, prob = probs)
# [1] 7 10 9 5 8
# Higher values are more likely to be chosen