lundi 21 novembre 2022

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)

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.




Aucun commentaire:

Enregistrer un commentaire