Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 204922

Simultaneously permute rows and columns while constraining by other factors

$
0
0

I am working in R with a dataset that has this structure:

plt <- c("W","W","W","W","W","W","W","W","W",
         "Z","Z","Z","Z","Z","Z","Z","Z","Z")
siteA <- c("a","a","a","b","b","b","c","c","c",
           "l","l","l","m","m","m","n","n","n")
siteB <- c("a","b","c","a","b","c","a","b","c",
           "l","m","n","l","m","n","l","m","n")
val <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18)
mydat <- cbind(plt,siteA, siteB, val)

And looks like this:

> mydat
      plt siteA siteB val 
 [1,] "W""a""a""1" 
 [2,] "W""a""b""2" 
 [3,] "W""a""c""3" 
 [4,] "W""b""a""4" 
 [5,] "W""b""b""5" 
 [6,] "W""b""c""6" 
 [7,] "W""c""a""7" 
 [8,] "W""c""b""8" 
 [9,] "W""c""c""9" 
[10,] "Z""l""l""10"
[11,] "Z""l""m""11"
[12,] "Z""l""n""12"
[13,] "Z""m""l""13"
[14,] "Z""m""m""14"
[15,] "Z""m""n""15"
[16,] "Z""n""l""16"
[17,] "Z""n""m""17"
[18,] "Z""n""n""18"

It is basically a stacked edge list of 2 site by site matrices (W and Z):

W
     a    b    c
a    1    4    7
b    2    5    8
c    3    6    9

Z
     l    m    n
l   10   13   16
m   11   14   17
n   12   15   18 

What I would like to do is run multiple restricted permutations in my "mydat" dataset such that values are only shuffled within each matrix and the permutation simultaneously shuffles both the rows and columns in each matrix. Given the size of the dataset I would avoid splitting it into individual matrices. However, for illustration purposes, an expected permutation output would look like this (when viewed as matrices):

  W
         c    b    a
    c    9    6    3
    b    8    5    2
    a    7    4    1

   Z
         m    l    n
    m   14   11   15
    l   13   10   16
    n   15   12   18 

I have considered using the permute package for this since it allows me to restrict the permutation by my "plt" variable while it also offers the type="grid" option to keep rows and columns together. However I am unable to configure it so that the rows and columns are both permuted together and, therefore, I am only able to get an output like this (when viewed as matrices):

  W
         a    c    b
    b    2    8    5
    c    3    9    6
    a    1    7    4

   Z
         m    n    l
    l   13   16   10
    n   15   18   12
    m   14   15   11

As shown above, the rows and columns in each matrix are moved to a different place. I have found a solution with similar results here.

After this long explanation, I was wondering if there was any way to restrict a permutation along an edge list and make it such that it corresponds to the simultaneous shuffling of rows and columns?

Thank you very much!


Viewing all articles
Browse latest Browse all 204922

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>