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

前端 未结 8 996
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

    0 讨论(0)
  • 2020-11-28 23:44

    Random Samples and Permutations ina dataframe If it is in matrix form convert into data.frame use the sample function from the base package indexes = sample(1:nrow(df1), size=1*nrow(df1)) Random Samples and Permutations

    0 讨论(0)
提交回复
热议问题