Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在r中模拟未知分布的随机数_R_Random_Simulation - Fatal编程技术网

在r中模拟未知分布的随机数

在r中模拟未知分布的随机数,r,random,simulation,R,Random,Simulation,我试图模拟R中未知分布的随机数。我知道随机数生成函数,如rnorm、runif和其他函数用于已知分布。我的输入数据是4年的收入数据,大约有500行。我想为这个收入分配生成10个随机数 谢谢如果您的数据在数据框中,您可以使用sample对整个年份进行随机抽样 df = data.frame(year1 = c(50,100,500), year2 = c(200, 500, 1000), year3 = c(250, 80, 99), year4=c(400, 5000, 98750)) samp

我试图模拟R中未知分布的随机数。我知道随机数生成函数,如rnorm、runif和其他函数用于已知分布。我的输入数据是4年的收入数据,大约有500行。我想为这个收入分配生成10个随机数


谢谢

如果您的数据在数据框中,您可以使用sample对整个年份进行随机抽样

df = data.frame(year1 = c(50,100,500), year2 = c(200, 500, 1000), year3 = c(250, 80, 99), year4=c(400, 5000, 98750))
sample(df, 2)
  year2 year4
1   200   400
2   500  5000
3  1000 98750
如果数据在矩阵中,可以使用sample对单个条目进行随机抽样

mat = matrix(rbind(c(50,100,500), c(200, 500, 1000), c(250, 80, 99),c(400, 5000, 98750)), ncol = 4)
sample(mat, 5)
[1] 5000   99  200  500   80
如果您想从特定列中采样,请查看?sample…或sampledf$revenue_stream,2,replace=TRUE对于OP的用例来说,使用替换进行采样可能更有意义