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.
CodePudding user response:
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