Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

r - Generate a set of random unique integers from an interval

I am trying to build some machine learning models,

so I need training data and a validation data

so suppose I have N number of examples, I want to select random x examples in a data frame.

For example, suppose I have 100 examples, and I need 10 random numbers, is there a way (to efficiently) generate 10 random INTEGER numbers for me to extract the training data out of my sample data?

I tried using a while loop, and slowly change the repeated numbers, but the running time is not very ideal, so I am looking for a more efficient way to do it.

Can anyone help, please?

question from:https://stackoverflow.com/questions/17773080/generate-a-set-of-random-unique-integers-from-an-interval

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

sample (or sample.int) does this:

sample.int(100, 10)
# [1] 58 83 54 68 53  4 71 11 75 90

will generate ten random numbers from the range 1–100. You probably want replace = TRUE, which samples with replacing:

sample.int(20, 10, replace = TRUE)
# [1] 10  2 11 13  9  9  3 13  3 17

More generally, sample samples n observations from a vector of arbitrary values.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...