按行和列排列数据帧 [英] permuting a data frame by rows and columns

查看:82
本文介绍了按行和列排列数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何将数据框中的所有数据随机化? 我的意思是,我将得到一个新的数据框,其中的数据按行和列进行排列,以获得一个具有与第一个相同编号的临时性新数据框.

Someone know how can I randomize all the data inside my dataframe? I mean, I would get a new data frame where data are permuted by rows and by columns, to obtain an aleatory new data frame with the same numbers that I have in the first.

类似这样的东西:

谢谢!

推荐答案

在矩阵上执行此操作会快很多:

It would be a lot faster to do this on a matrix:

dm <- matrix(1:25, ncol = 5); dm
dm[] <- sample(dm); dm

这是错误的:我很确定首先对列进行排列,然后对行进行排列,将获得与对整个向量进行排列然后重塑为原始尺寸相同的结果." < \ s>

This is wrong: "I'm pretty sure that permuting first on columns and then on rows should give you the same result as permuting the entire vector and then reshaping to the original dimensions." <\s>

辛普森方法"会给出不同的结果,并且可能是所要求的(但如果将其作为仿真工作的一部分,则使用矩阵测试床会更快):

The "Simpson method" would give different results and may be what was requested (but it will be faster with a matrix testbed if this it to be done as part of a simulation effort):

 dm <- dm[ sample(nrow(dm)), sample( ncol(dm)) ]

这篇关于按行和列排列数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆