mercredi 24 juillet 2019

Grouping (Assigning Categorical Variable) to every 100 observations for a list of 10,000 observations in R

I am just experimenting with R. My initial project was inspired from Hands On Programming with R.

I created a data frame with 10,000 rolls of two 6-sided dice.

Now, I want to see for each set of consecutive 100 rolls, what is the percent of doubles.

I have started to write a nested ifelse, but it is super tedious.

rolls.df <- rolls.df %>%
     mutate(run = ifelse(roll.num <= 100, 1,
                         ifelse(roll.num >100 & roll.num <= 200,2,
                               ifelse(roll.num >200 & roll.num <= 300,3,
                                  ifelse(roll.num >300 & roll.num <= 400,4,
                                      ifelse(roll.num >400 & roll.num <= 500,5,0))))))

I got the ifelse to work, but I didn't want to continue - first I wanted to learn how to do this more efficiently. Thanks

Aucun commentaire:

Enregistrer un commentaire