[R] Three values that add to the same number by 0.01 steps

arun smartpink111 at yahoo.com
Tue Jan 28 18:04:31 CET 2014

May be this helps:
 mat1 <- matrix(sample(seq(0,1,by=0.01),360000,replace=TRUE),ncol=3)
mat2 <- mat1[sprintf("%.2f",rowSums(mat1))=="1.00",]
#[1] FALSE


I'd like to create a matrix with three columns so that each 
element is between 0 and 1 and each row always adds to 1. So, if in the 
same row the columns are x1, x2, and x3 and x1 =1, then x2 = 0 and x3 = 
0. Or if x1 = .03, then x2 = .97 and x3 = 0. Or... x1 = .05, x2 = .01, 
x3 = .94. 

Basically so it accounts for all values of x1,x2,x3 that add up to 1 with the sequence by 0.01. 

I tried a nested seq (if that is even possible), but that didn't work. 

Is there a way to do this? Perhaps a package that I don't know about?

More information about the R-help mailing list