jeudi 5 juillet 2018

randomly ordering across groups (not within group) in data.table

Let's say I want to order the iris dataset (as a data.table) by Species, keeping observations grouped by species and randomly ordering across species.

How do I do that?

I am not talking about generating a random order within groups (species).

My intuition was to write the code bellow. But it actually creates the within species random variable. Well at least it makes the question reproducible

d <- iris %>% data.table
set.seed('12345')
d[,g:=runif(.N),Species]




Aucun commentaire:

Enregistrer un commentaire