从数据框中选择随机数 [英] Random number selection from a data-frame
问题描述
我按照 Bernaard & 概述的步骤创建了一个错误"数据框.Sijtsma (2000) 缺失数据插补的双向方法.为了完成我对缺失数据的计算,我需要从这个错误数据框中随机选择一个 SINGLE NUMBER 并将其添加到我已经计算出的缺失数据值中.
I have created a dataframe of "errors" following the steps outlined by Bernaard & Sijtsma's (2000) two-way method for missing data imputation. In order to complete my calculation for missing data, I need to make a random selection of a SINGLE NUMBER from this error dataframe and add it to my already calculated missing data values.
我熟悉 sample() 函数,但我不是在寻找一行或一列的随机样本,而是从数据框中寻找一个单独的单元格.有没有一种简单的方法可以做到这一点,例如单个选择随机数()"命令?有没有我尚未探索的替代方法?
I am familiar with the sample() function, but I am not looking for a random sample of a row or a column, but rather one individual cell from a data-frame. Is there a simple way to do this, such as a single "select random number()" command? Is there an alternative method I have yet to explore?
非常感谢任何帮助.
推荐答案
如果您可以转换为 matrix
而不是 dataframe
会更容易,但前提是假设您需要保留不同的数据类型或某些此类限制,
It's easier if you can convert to a matrix
instead of a dataframe
, but on the assumption that you need to keep different data types or some such limitation,
foo<-as.data.frame(matrix(runif(20),nrow=4,ncol=5))
foo[sample(1:nrow(foo)),sample(1:ncol(foo))]
将选择一个随机元素.
这篇关于从数据框中选择随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!