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 split a number into parts by a number that it is getting divided by – R

I am looking at a specific use case where I would like to split a number into parts by a number that it is getting divided by
For example : 42/8 = 8, 8, 8, 8, 8, 2 or 68/8 = 8, 8, 8, 8, 8, 8, 8, 8, 4

>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 could do…

a <- 42
b <- 8

paste(c(rep(b, a %/% b), a %% b), collapse = ",")

[1] "8,8,8,8,8,2"

Or don’t use the paste() if you just want a vector of numbers.

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