Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to create a list of values from a function and then make those values into a histogram in R? I keep getting NA

I am new to the language R. I have a function and am trying to make a list that uses that function to create 1000 entries to the list, to then make a histogram.

This is my code that I have tried:

for (i in 1:1000)
   zscores[i] <- Reduce("+", runif(12)) - 6
hist(zscores)

I am getting an number for i[1] and i[1000], but for all the other entries I get NA when I should be getting a number. What am I doing wrong?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I didn’t get any NAs running your code this way

zscores <- c()
for (i in 1:1000) {
  zscores[i] <- Reduce("+", runif(12)) - 6
}
hist(zscores)

Or running the way Gregor Thomas mention

zscores <- c()
for (i in 1:1000) {
  zscores[i] <- sum(runif(12)) - 6
}
hist(zscores)

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading