jeudi 1 octobre 2020

Sample from a dataframe with specified probabilities in R

Below, I first group my data.frame (d) by a 5-level categorical variable called sector (Education, Industry, NGO, Private, Public). Then, I want to sample from each level of sector with the following probabilities: c(.2, .3, .4, .1).

I'm using the code below to achieve my goal without success? Is there a fix for that?

library(tidyverse)

d <- read.csv('https://raw.githubusercontent.com/rnorouzian/d/master/su.csv')

set.seed(1)
(out <- d %>%
  group_by(sector) %>%
  slice_sample(n = 2, weight_by = c(.2, .3, .4, .1))) # `Error: incorrect number of probabilities`



Aucun commentaire:

Enregistrer un commentaire