HelloGoodBye
HelloGoodBye

Reputation: 1

Permutation test for paired data using a for loop

I want to permutate my data, which is paired, using a for loop. I think I'm supposed to randomly flip some of my pairs in this case, but I can't figure out how to do that exactly. I already searched the internet for this, and I found ways to permutate this kind of data, but I didn't find it for this kind of method. An example of what I think I'm supposed to do:

Example dataset:

   day1   day2
 1  5.5   1.5  
 2  2.5   6.5  
 3  7.5   8.5  
 4  4.5   1.5  
 5  5.5   1.5  

and I want it to randomly swap some of the rows, for example this:

   day1   day2
 1  5.5   1.5  
 2  6.5   2.5 
 3  7.5   8.5  
 4  1.5   4.5  
 5  5.5   1.5  

I've tried using the sample() command, but that seems to swap the whole rows instead of only some of them. So how do you randomly swap some of them?

Upvotes: 0

Views: 242

Answers (3)

akrun
akrun

Reputation: 887058

We can use pmap from purrr

library(dplyr)
library(purrr)
df %>%
   pmap_dfr(~ sample(c(...)))

-output

# A tibble: 5 x 2
#   day2  day1
#  <dbl> <dbl>
#1   1.5   5.5
#2   6.5   2.5
#3   8.5   7.5
#4   1.5   4.5
#5   1.5   5.5

data

df <- structure(list(day1 = c(5.5, 2.5, 7.5, 4.5, 5.5), day2 = c(1.5, 
6.5, 8.5, 1.5, 1.5)), class = "data.frame", row.names = c(NA, 
-5L))

Upvotes: 1

Allan Cameron
Allan Cameron

Reputation: 173793

Here's a one-liner option that will shuffle elements in the first and second columns. It will also work if there are several columns to swap:

setNames(as.data.frame(t(apply(df, 1, sample))), names(df))
#>   day1 day2
#> 1  1.5  5.5
#> 2  2.5  6.5
#> 3  8.5  7.5
#> 4  4.5  1.5
#> 5  5.5  1.5

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 388947

If you want to randomly swap the columns of certain rows try :

set.seed(6781)
replace_inds <- sample(c(TRUE, FALSE), nrow(df), replace = TRUE)
df[replace_inds, ] <- df[replace_inds, 2:1]
df

#  day1 day2
#1  5.5  1.5
#2  2.5  6.5
#3  8.5  7.5
#4  4.5  1.5
#5  1.5  5.5

data

df <- structure(list(day1 = c(5.5, 2.5, 7.5, 4.5, 5.5), day2 = c(1.5, 
6.5, 8.5, 1.5, 1.5)), class = "data.frame", row.names = c(NA, -5L))

Upvotes: 1

Related Questions