Home > Net >  ASK: Generate positive values only in Normal Distribution (rnorm)
ASK: Generate positive values only in Normal Distribution (rnorm)

Time:09-26

Is it possible to condition a rnorm function? For example, I want to include upper/lower limits for a normally distributed set (from 1 to 100) and all values are integers (suppose it describes a person's age). In that case, how does the code look like?

CodePudding user response:

There is a package called truncnorm, that has some functions to use a Truncated Normal Distribution.

#install.packages("truncnorm")
library(truncnorm)

x <- rtruncnorm(n = 1000,a = 1,b = 100,mean = 50,sd = 5)

plot(density(x))

enter image description here

To make them integers, you can use round

y <- round(x)

plot(density(y))

enter image description here

  •  Tags:  
  • r
  • Related