[R] permutation within rows of a matrix

R. Michael Weylandt michael.weylandt at gmail.com
Thu Nov 17 00:33:21 CET 2011


Seems logical to me -- and, Juan, sorry for messing up earlier -- read
too hastily.

Michael

On Wed, Nov 16, 2011 at 5:55 PM, Peter Ehlers <ehlers at ucalgary.ca> wrote:
> I must be missing something. What's wrong with
>
>  t(apply(mat, 1, sample))
>
> ?
>
> Peter Ehlers
>
> On 2011-11-16 12:12, Gavin Simpson wrote:
>>
>> On Wed, 2011-11-16 at 14:29 -0500, R. Michael Weylandt wrote:
>>>
>>> Suppose your matrix is called X.
>>>
>>> ? sample
>>> X[sample(nrow(X)),]
>>
>> That will shuffle the rows at random, not permute within the rows.
>>
>> Here is an alternative, first using one of my packages (permute -
>> shameful promotion ;-) !:
>>
>> mat<- matrix(sample(0:1, 100, replace = TRUE), ncol = 10)
>>
>> require(permute)
>> perms<- shuffleSet(10, nset = 10)
>> ## permute mat
>> t(sapply(seq_len(nrow(perms)),
>>          function(i, perms, mat) mat[i, perms[i,]],
>>          mat = mat, perms = perms))
>>
>> If you don't want to use permute, then you can do this via standard R
>> functions
>>
>> perms<- t(replicate(nrow(mat), sample(ncol(mat))))
>> ## permute mat
>> t(sapply(seq_len(nrow(perms)),
>>          function(i, perms, mat) mat[i, perms[i,]],
>>          mat = mat, perms = perms))
>>
>> HTH
>>
>> G
>>
>>> Michael
>>>
>>> On Wed, Nov 16, 2011 at 11:45 AM, Juan Antonio Balbuena<balbuena at uv.es>
>>>  wrote:
>>>>
>>>> Hello
>>>> This is probably a basic question but I am quite new to R.
>>>>
>>>> I need to permute elements within rows of a binary matrix, such as
>>>>
>>>>     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
>>>>  [1,]    0    0    0    0    1    0    0    0    0     0
>>>>  [2,]    0    0    1    1    0    0    0    1    1     0
>>>>  [3,]    0    1    0    0    0    0    1    0    0     0
>>>>  [4,]    0    0    0    0    0    0    1    1    0     0
>>>>  [5,]    0    0    0    1    0    0    0    0    1     0
>>>>  [6,]    0    0    1    1    0    0    0    0    0     1
>>>>  [7,]    0    0    0    0    0    0    0    0    0     0
>>>>  [8,]    1    1    0    1    0    0    0    1    0     1
>>>>  [9,]    1    0    0    1    0    1    0    1    0     0
>>>> [10,]    0    0    0    0    0    0    0    1    0     1
>>>>
>>>>
>>>> That is, elements within each row are permuted freely and independently
>>>> from
>>>> the other rows.
>>>>
>>>> I see that is is workable by creating a array for each row, performing
>>>> sample and binding the arrays again, but I wonder whether there is a
>>>> more
>>>> efficient way of doing the trick.
>>>>
>>>> Any help will be much appreciated.
>>>>
>>>>
>>>>
>>>> --
>>>> View this message in context:
>>>> http://r.789695.n4.nabble.com/permutation-within-rows-of-a-matrix-tp4076989p4076989.html
>>>> Sent from the R help mailing list archive at Nabble.com.
>>>>
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide
>>>> http://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>
>



More information about the R-help mailing list