Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Creating a vector with a specific amount of random numbers

Tags:

r

vector

I was hoping someone could help point me in the right direction to create a vector in R, containing a defined amount of randomly generated numbers. I am a complete newbie to R, and I have learned that the concatenate function is used for creating vectors. However, I wish to populate the vector with 50 random numbers. I do not wish to specify a range or any other conditions for the numbers.

MyVectorObject <- c(...)

Any suggestions would be greatly appreciated!

like image 465
Data Medici Avatar asked Feb 06 '15 23:02

Data Medici


People also ask

How do I generate 5 random numbers in R?

Random numbers from a normal distribution can be generated using runif() function. We need to specify how many numbers we want to generate. Additionally we can specify the range of the uniform distribution using max and min argument. If not provided, the default range is between 0 and 1 .

How do I randomly select numbers in R?

To generate random numbers from a uniform distribution you can use the runif() function. Alternatively, you can use sample() to take a random sample using with or without replacements.

How do you generate a random number from a uniform distribution in R?

The runif() function generates random deviates of the uniform distribution and is written as runif(n, min = 0, max = 1) .


1 Answers

It depends on which numbers you want to generate. These are some options.

x1 <- rpois(n = 50, lambda = 10)
x2 <- runif(n = 50, min = 1, max = 10)
x3 <- sample(x = c(1, 3, 5), size = 50, replace = TRUE)
like image 59
Michele Usuelli Avatar answered Nov 15 '22 05:11

Michele Usuelli