|
|
This post has NOT been accepted by the mailing list yet.
Hi,
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?
|
|
Hi,
May be this helps:
set.seed(85)
mat1 <- matrix(sample(seq(0,1,by=0.01),360000,replace=TRUE),ncol=3)
mat2 <- mat1[sprintf("%.2f",rowSums(mat1))=="1.00",]
any(!rowSums(mat2))
#[1] FALSE
A.K.
Hi,
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?
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
|
|
> 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
You could start with expand.grid
m <- expand.grid(x1=0:100, x2=0:100) #Avoids comparing floats
m <- m[rowSums(m)<=100,] #Throw away the oversized ones
m <- cbind(m, x3=100-rowSums(m)) #Get the final column
m <- m/100 #Scale to [0,1]
table(rowSums(m))
S Ellison
*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
|
|
An alternative using runif.
x <- round(runif(10000, 0, 1), 2)
y <- round(runif(10000, 0, 1-x), 2)
z <- round(1-x-y, 2)
sum1 <- cbind(x, y, z)
any(!(sum1[,1] + sum1[,2] + sum1[,3]))
Richard
On Tue, Jan 28, 2014 at 9:36 AM, S Ellison < [hidden email]> wrote:
> > 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
>
> You could start with expand.grid
>
> m <- expand.grid(x1=0:100, x2=0:100) #Avoids comparing floats
> m <- m[rowSums(m)<=100,] #Throw away the oversized ones
> m <- cbind(m, x3=100-rowSums(m)) #Get the final column
> m <- m/100 #Scale to
> [0,1]
>
> table(rowSums(m))
>
>
> S Ellison
>
>
>
> *******************************************************************
> This email and any attachments are confidential. Any u...{{dropped:13}}
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
|
|
Also,
library(gtools)
x <- rdirichlet(1000, c(1,1,1) )
any(!rowSums(x))
#[1] FALSE
A.K.
On Tuesday, January 28, 2014 2:20 PM, Richard Kwock < [hidden email]> wrote:
An alternative using runif.
x <- round(runif(10000, 0, 1), 2)
y <- round(runif(10000, 0, 1-x), 2)
z <- round(1-x-y, 2)
sum1 <- cbind(x, y, z)
any(!(sum1[,1] + sum1[,2] + sum1[,3]))
Richard
On Tue, Jan 28, 2014 at 9:36 AM, S Ellison < [hidden email]> wrote:
> > 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
>
> You could start with expand.grid
>
> m <- expand.grid(x1=0:100, x2=0:100) #Avoids comparing floats
> m <- m[rowSums(m)<=100,] #Throw away the oversized ones
> m <- cbind(m, x3=100-rowSums(m)) #Get the final column
> m <- m/100 #Scale to
> [0,1]
>
> table(rowSums(m))
>
>
> S Ellison
>
>
>
> *******************************************************************
> This email and any attachments are confidential. Any u...{{dropped:13}}
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
|
|
This post has NOT been accepted by the mailing list yet.
Thanks everyone!
I'll give them each a try
|
|