[Solved] How can I introduce values to a vector in random positions in R?


If you mean to replace 1000 elements in the x vector with values between 500 and 700, you first need to generate these 1000 elements:

r <- runif(1000, min=500, max=700)

I am assuming here that random values are uniformly between 500 and 700.

Then you need to select places to put these values in:

idx <- sample(10000, 1000)

Finally, replace the values at these places:

x[ idx ] <- r

Finally, to see the results of your action:

hist(x)

It should look like: Histogram of x

2

solved How can I introduce values to a vector in random positions in R?