Shuffle one column based on factors in another R

I created a data block to illustrate my problem. I am relatively new to R.

#### permutation problem a <- c("beagle", "beagle", "beagle", "basset", "basset") b <- c(44, 33, 22, 34, 42) c <- c(1:5) d <- c(7:11) dogframe <- data.frame(cbind(a,b,c,d)) output > dogframe abcd 1 beagle 44 1 7 2 beagle 33 2 8 3 beagle 22 3 9 4 basset 34 4 10 5 basset 42 5 11 > 

What I want to do is randomly shuffle column b by factors in column a. Thus, the values ​​44.33 and 22 will be shuffled for the Beagle, and 34 and 42 will be shuffled for the basset. I want the result to be a data framework similar to the original, with only shuffled values ​​in column b.

Thanks.

+6
source share
3 answers

Like this:

 dogframe$b <- ave(dogframe$b, dogframe$a, FUN = sample) 

which you can also write:

 dogframe$b <- with(dogframe, ave(b, a, FUN = sample)) 
+5
source

Well, you already have base and plyr . Here is the third option in such matters:

 require(data.table) DT <- data.table(dogframe) DT[,b:=sample(b),by=a] 

This overwrites column b ; if you want it in a separate copy, you would do:

 DT2 <- copy(DT)[,b:=sample(b),by=a] 
+4
source

You can also do this with the plyr package, as follows:

 ddply(dogframe, "a", function(df) { df$b <- df$b[sample(nrow(df))]; df}) 

Which gives, for example:

  abcd 1 basset 42 4 10 2 basset 34 5 11 3 beagle 44 1 7 4 beagle 22 2 8 5 beagle 33 3 9 
+2
source

Source: https://habr.com/ru/post/954170/


All Articles