0

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.

The_Tams
  • 205
  • 1
  • 10
  • 3
    Just generate a random matrix and then normalize to the row sum. – Dan Adams Nov 21 '22 at 17:18
  • If you need the results to be integers: https://stackoverflow.com/questions/2161406/how-do-i-generate-a-uniform-random-integer-partition – Ben Bolker Nov 21 '22 at 17:22
  • 2
    Do they need to be integers? Or is non-negative the the only criteria? And what do you mean by "don't want to take them from a normal distribution"? I don't think that would work generally, but it seems weird to single out - are you interested in other distributions (Poisson or Exponential or Gamma or Uniform or ...)? – Gregor Thomas Nov 21 '22 at 17:23
  • They don't need to be integers, and I don't want them to be other distributions either. In the answer I referenced they filled the matrix from a normal distribution, which I did not want to do. – The_Tams Nov 21 '22 at 17:29

1 Answers1

1

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

Dan Adams
  • 4,971
  • 9
  • 28
  • I really like how elegantly simple this solution is. I was wading through looking for built in solutions in functions, when simple maths would do it! Thanks Dan – The_Tams Nov 21 '22 at 17:31