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 make a random number matrix in R where all rows = a given value?

I am trying to make a dummy dataset to test a model I have built on real data. I want to make a matrix of 11 columns, each row of which sums to 100 as its percentage data.

I can make a sample matrix of the right dimensions, for example, 100 rows by 11 columns

set.seed(42) ranMat <- matrix(sample(seq(0:100), 1100, replace = TRUE), ncol = 11)

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

but I don’t know where to start to ask it to make each row sum to 100.

The related examples I have found on stackoverflow are in languages I don’t know (i.e. Python and Matlab), and so can’t port over so to speak.
Close, but not the same are for example this question but I don’t want to take them from a normal distribution and I don’t want negative numbers.

>Solution :

I’d just divide the random matrix by its rowSums and multiply by 100.

set.seed(42) 
ranMat <- matrix(sample(seq(0:100), 1100, replace = TRUE), ncol = 11)

norm_ranMat <- ranMat / rowSums(ranMat) * 100

head(rowSums(norm_ranMat))
#> [1] 100 100 100 100 100 100

Created on 2022-11-21 with reprex v2.0.2

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