jeudi 31 janvier 2019

How to take a random sample from a data frame based on factors with restrictions in R?

I have been thinking about this for a while, I am sorry if this is too easy but I am just starting R.

I have a basic data set and want to create two "teams" based on three factor columns. I am trying to distribute players in proportionally according to some categories I defined, in this case

Jugador <- c("Player1","Player2","Player3","Player4","Player5","Player6","Player7","Player8","Player9","Player10","Player11","Player12","Player13")
RangoEdad <- c("3","2","2","3","3","2","2","2","3","3","1","1","1")
Sexo <- c("M","F","M","M","F","M","F","M","M","F","F","M","M")
Fam <- c("1","2","2","3","3","3","4","4","5","5","5","5","5")

draft <- cbind(Jugador, RangoEdad,Sexo,Fam)
draft.df <- as.data.frame(draft)

draft.df$RangoEdad <- as.factor(draft.df$RangoEdad)
draft.df$Fam <- as.factor(draft.df$Fam)

All I can think of now is creating selections of combinations of factors and then sampling and "drawing" from each one of them. But I believe there is a better solution.

The expected output should be two teams with at least 1 player from each category (when possible, Player 1 has own category).




Aucun commentaire:

Enregistrer un commentaire