Вопрос

I have a data.frame that looks like this (my real data.frame is bigger but the structure is similar):

df <- data.frame(ID=c(rep('A', 5), rep('B', 5), rep('C',5)), Score=c(1,1,0,0,0,1,1,1,0,0,1,1,1,0,0))

And I would like to obtain several randomized data.frames (e.g 100) where column Score is randomized and column ID remains the same, but I need to keep the same number of zeros and ones in `df$Score.

I've tried with:

df1 <- transform(df, Score=ave(Score, ID, FUN=function(b) sample(b, replace=T)))

but the proportions of 0s and 1s are not kept always,

Thanks

Это было полезно?

Решение

If you want to keep the 0-1 proportion within IDs, set replace=F (which is by default):

df1 <- transform(df, Score=ave(Score, ID, FUN=function(b) sample(b, replace=F)))

If you want to keep the overall 0-1 porportion, you can simply do this:

df1 <- data.frame(ID=df$ID, Score=sample(df$Score))
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top