I am trying to use the sample_frac command but cannot see the effect that the weight option is having. In the following example I expect that the weight argument should end up providing me with a sampled_data set with 30% of group A and 70% of group B but this is not the case. Any assistance would be extremely welcome.
library(tidyverse)
# Create sample data
set.seed(123) # for reproducibility
data <- data.frame(
id = 1:100,
group = sample(c("A", "B"), 100, replace = TRUE),
value = runif(100)
)
# Calculate desired sample percentages
data <- data %>%
group_by(group) %>%
mutate(
sample_percentage = case_when(
group == "A" ~ 0.3, # 30% for group A
group == "B" ~ 0.7 # 70% for group B
)
)
# Sample rows based on the calculated percentage
sampled_data <- data %>%
sample_frac(size = 0.5, replace = FALSE, weight = sample_percentage)