0

I want to create x randomised matrices where only the columns are permuted but the rows are kept constant. I already took a look at permatful() in the vegan package. Nevertheless, i was not able to generate the desired result even though i am quite sure that this should be possible somehow.

df = matrix(c(2,3,1,4,5,1,3,6,2,4,1,3), ncol=3)

This is (one possible) desired result

     [,1] [,2] [,3]
[1,]    2    5    2
[2,]    3    1    4
[3,]    1    3    1
[4,]    4    6    3

           v
           v permutation
           v

     [,1] [,2] [,3]
[1,]    5    2    2
[2,]    1    4    3
[3,]    3    1    1
[4,]    6    3    4

I tried something like permatfull(df, times=1, fixedmar = "rows", shuffle = "samp") which results in

     [,1] [,2] [,3]
[1,]    5    2    2
[2,]    1    4    3
[3,]    3    1    1
[4,]    3    4    6

Now column 1 (originally column 2) has changed from 5,1,3,6 to 5,1,3,3.

Anyone an idea why I do not get the expected result?

Thanks in Advance,

Christian

MrNetherlands
  • 920
  • 7
  • 14
  • 1
    You can just do `df[,sample(ncol(df))]` to randomly shuffle the columns – Andrew Gustar Jul 13 '17 at 13:27
  • True, but i have to do it multiple times and i thought there must be a more sophisticated way than calling your function in a for loop – MrNetherlands Jul 13 '17 at 13:31
  • 1
    `dfshuffle <- lapply(1:n,function(i) df[,sample(ncol(df))])` will give you a list of `n` random shuffles. – Andrew Gustar Jul 13 '17 at 13:35
  • The [Andrew Guster](https://stackoverflow.com/users/7727429/andrew-gustar) is a good approach. For more readability you can define the function before use `lapply` instead of use an anonymous function. `tranpose <- function(X) { col_index <- sample(ncol(X)) X[, col_index]}` – Cristóbal Alcázar Jul 13 '17 at 13:39
  • @AndrewGustar Thanks for this alternative solution! Nevertheless i am still interested if this is also possible with the `permatfull` function – MrNetherlands Jul 13 '17 at 15:35
  • I've had a look at the documentation for `permatfull` and it says that 'If rows or columns are fixed, cells within rows or columns are randomised' - which sounds as if it is shuffling each row independently, rather than shuffling the columns (i.e. shuffling each row in the same way). That would explain the result you got. It might be that `permatswap` is the function you need, although I've not tried it. – Andrew Gustar Jul 13 '17 at 16:25
  • No, you cannot use `permatfull` or `permatswap` for this task. They are functions to generate ecological null models (*sensu* Gotelli), but you want to have permutations of columns which is quite a different task. – Jari Oksanen Jul 14 '17 at 18:19

0 Answers0