Home > Software engineering >  How to use permutation test in R for a specific column for a specific number of times?
How to use permutation test in R for a specific column for a specific number of times?

Time:11-28

Lets say I have the following df:

activity        score
1                 2
2                 4
3                 1
4                 6
5                 10
6                 2
7                 3
8                 6
9                 8
10                5
11                6
12                8

and I wanted to randomly shuffle every three scores until it hits activity 12 and then restart the loop (i.e. restart at 1 and reshuffle again every three scores until it hits 12, n number of times). How would I do this in R? Keep in mind, the activities cannot move only the scores.

Here would be an example of a result df
activity        score
1                 4
2                 2               <- reshuffled
3                 1

4                 2
5                 10               <- reshuffled
6                 6

7                 3 
8                 6               <- reshuffled
9                 8

10                8
11                6               <- reshuffled
12                5

CodePudding user response:

Assume dt is the dataframe,

for(i in 1:100){
  dt[,2] <- do.call(c,lapply(seq(1,12,by=3),function(iter){
    sample(dt[iter:(iter 2),2])
  }))
}
  • Related