Delphine
Delphine

Reputation: 1163

Random sampling

I would like to know how to implement a way to get a random sub-sample within a larger sample in R using a large collection of true random numbers (obtained using a quantum generator) those are integers which can have multiple occurrences.

__

Edit: Solution.

Since I needed a remise and my generated numbers in a float64 were finally unique (due to the high precision), I have used the following solution :

1) generate as many numbers as length(data)

2)

temp<-cbind(data,randomnb)
randomizeddata<-res[order(res[,2])]

3) split the dataset

Upvotes: 0

Views: 3147

Answers (3)

NPE
NPE

Reputation: 500873

Let's say v is your data and r are the true random numbers (scaled so that they range from 0 to 1):

> v <- runif(100)
> r <- runif(10) # using psedo-random numbers for demo purposes
> v[r * length(v) + 1]

This selects ten random elements from v (with replacement).

Upvotes: 1

digEmAll
digEmAll

Reputation: 57220

What about sample function ?

e.g.

set.seed(3) # just to get the same result
x <- 1:10
sample(x,10)
# print: 2  8  4  3  9  6  1  5 10  7

Upvotes: 0

Richie Cotton
Richie Cotton

Reputation: 121157

For true random numbers, use randomNumbers from the random package.

r <- randomNumbers(number_of_samples, max = nrow(your_data), col = 1)
your_data[r, ]

Upvotes: 6

Related Questions