user1740546
user1740546

Reputation: 11

Limit Decimal Places In Generated Numbers in R.Studio

I am a beginner in programmin in general and R specifically.

I would like to generate a set of random numbers in a normal distribution but to limit the decimal places in these numbers to only 2.

I have been using x1 <- runif() to generate my numbers.

Can I add something to it to enable me to only get results rounded off to 2 decimal places?

Upvotes: 1

Views: 5392

Answers (1)

Andrew Sunderland
Andrew Sunderland

Reputation: 31

You can limit the decimal places using the round() function.

If I understand your question correctly this should do the trick:

x1 <- round(
  runif(5, min=0, max=1)   
  , digits = 2
)
x1

The results, which will be different each time, are:

[1] 0.55 0.55 0.75 0.85 0.13

Upvotes: 3

Related Questions