How to randomize (or permute) a dataframe rowwise and columnwise?

前端 未结 8 1001
Happy的楠姐
Happy的楠姐 2020-11-28 22:50

I have a dataframe (df1) like this.

     f1   f2   f3   f4   f5
d1   1    0    1    1    1  
d2   1    0    0    1    0
d3   0    0    0    1    1
d4   0             


        
8条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-28 23:42

    you can also use the randomizeMatrix function in the R package picante

    example:

    test <- matrix(c(1,1,0,1,0,1,0,0,1,0,0,1,0,1,0,0),nrow=4,ncol=4)
    > test
         [,1] [,2] [,3] [,4]
    [1,]    1    0    1    0
    [2,]    1    1    0    1
    [3,]    0    0    0    0
    [4,]    1    0    1    0
    
    randomizeMatrix(test,null.model = "frequency",iterations = 1000)
    
         [,1] [,2] [,3] [,4]
    [1,]    0    1    0    1
    [2,]    1    0    0    0
    [3,]    1    0    1    0
    [4,]    1    0    1    0
    
    randomizeMatrix(test,null.model = "richness",iterations = 1000)
    
         [,1] [,2] [,3] [,4]
    [1,]    1    0    0    1
    [2,]    1    1    0    1
    [3,]    0    0    0    0
    [4,]    1    0    1    0
    > 
    

    The option null.model="frequency" maintains column sums and richness maintains row sums. Though mainly used for randomizing species presence absence datasets in community ecology it works well here.

    This function has other null model options as well, check out following link for more details (page 36) of the picante documentation

提交回复
热议问题