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

前端 未结 8 1010
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条回答
  •  猫巷女王i
    2020-11-28 23:35

    Of course you can sample each row:

    sapply (1:4, function (row) df1[row,]<<-sample(df1[row,]))
    

    will shuffle the rows itself, so the number of 1's in each row doesn't change. Small changes and it also works great with columns, but this is a exercise for the reader :-P

提交回复
热议问题