What's the equivalent of R sample function in Stata

孤街醉人 提交于 2019-12-11 17:50:23

问题


I have a basic question about Stata. I have programming experience in R but I've started a new job where Stata is the main language. I'm currently diving into Stata on my own and sometimes it's hard to understand how to do simple things.

I've trying to get 5 random numbers between 3 and 50 but without success.

In R, any of these would work:

 floor(runif(5, min=3, max=50))
 16 39 11 11  5 # output

 sample(3:50, 5, replace=TRUE)
 28 13  5 36 19 # output

But I'm not sure how to do this in Stata, specifically how to return random numbers within the desired range (3:50). Any pointers would be appreciated. I found the runiform() function but I don't think I can get the same output.


回答1:


Is this what you want?

set obs 5
generate rnum = runiform(3, 50)

You are basically creating a dataset first and then generating a variable with the desired properties.



来源:https://stackoverflow.com/questions/48441167/whats-the-equivalent-of-r-sample-function-in-stata

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!