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 recurring function in R to sample 35 sets of 2-digit random numbers

I am currently learning R programming, and I was tasked to generate a set of 20 two-digit random numbers. I know that I can paste sample(0:9, 2, replace=TRUE) in 20 lines, but I was wondering if there’s a way to make a recurring function (like in Python) so that the code will be shorter. I searched how to create recurring functions in R, but I can’t figure out how to count the generated numbers so that I can assign an if statement to terminate it. I would very much appreciate any help. Thank you!

>Solution :

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

You can use –

set.seed(2301)
sprintf('%02i', sample(99, 20))
#[1] "52" "93" "27" "01" "80" "37" "16" "83" "46" "32" "39" "57" "59" 
#    "26" "89" "75" "17" "70" "12" "90"

Or with two samples

set.seed(1422)
paste(sample(0:9, 20, replace = TRUE), sample(0:9, 20, replace = TRUE), sep = '')
#[1] "88" "71" "36" "01" "31" "92" "72" "61" "72" "39" "21" "03" "39" 
#    "74" "68" "87" "95" "92" "16" "34"
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