1

I'm trying to use sample function however encounturing some trouble. My objective is to have 500 samples from a normal distrubition and replace any numbers that are less than 5. I tried using replace function but not familiar with syntax and keep recieivng errors.

My normal distrubition code is:

 x <- rnorm(1000,10,4)
M.Ustun
  • 289
  • 2
  • 6
  • 16

2 Answers2

2

So it seems like you actually want a truncated normal distribution. I would suggest truncdist. Then you can do

library(truncdist)
set.seed(123)
x <- rtrunc(1000, "norm", a = 5, mean = 10, sd = 4)

to get the samples you want:

summary(x)

#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#  5.009   8.268  10.440  10.780  13.002  23.091 

plot(density(x, from = 5))

enter image description here

duckmayr
  • 16,303
  • 3
  • 35
  • 53
1

Remove values smaller than 5 from x and then sample 500 values

sample(x[x > 5], 500)
lop
  • 91
  • 3