Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a 100 number vector with random values in R rounded to 2 decimals

Tags:

r

I need to do a pretty simple task,but since im not versed in R I don't know exactly how to. I have to create a vector of 100 numbers with random values from 0 to 1 with 2 DECIMAL numbers. I've tried this:

 x2 <- runif(100, 0.0, 1.0)

and it works great, but the numbers have 8 decimal numbers and I need them with only 2.

like image 516
Pablo Romero Avatar asked Jul 21 '13 12:07

Pablo Romero


People also ask

How do I create a random number set in R?

For uniformly distributed (flat) random numbers, use runif() . By default, its range is from 0 to 1. To generate numbers from a normal distribution, use rnorm() . By default the mean is 0 and the standard deviation is 1.


2 Answers

Perhaps also:

(sample.int(101,size=100,replace=TRUE)-1)/100
like image 92
Blue Magister Avatar answered Oct 22 '22 10:10

Blue Magister


So you want to sample numbers randomly from the set { 0, 1/100, 2/100, ..., 1 }? Then write exactly that in code:

hundredths <- seq(from=0, to=1, by=.01)
sample(hundredths, size=100, replace=TRUE)
like image 39
Ryan C. Thompson Avatar answered Oct 22 '22 12:10

Ryan C. Thompson