[R] generate
David Carlson
dcarlson at tamu.edu
Thu Mar 27 21:44:33 CET 2014
Or tweaking Don's solution but using the original mu and sig
vectors:
> set.seed(42)
> x.matrix<-matrix(0, nrow=27, ncol=5)
> for(i in 1:27){
+ x.matrix[i, ] <- rnorm(5, mu[i], sig[i])
+ }
>
> set.seed(42)
> xmat <- matrix(rnorm(27*5, rep(mu, each=5), rep(sig, each=5)),
+ nrow=27, byrow=TRUE)
> identical(x.matrix, xmat)
[1] TRUE
-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352
-----Original Message-----
From: r-help-bounces at r-project.org
[mailto:r-help-bounces at r-project.org] On Behalf Of MacQueen, Don
Sent: Thursday, March 27, 2014 3:09 PM
To: IZHAK shabsogh
Cc: r-help at r-project.org
Subject: Re: [R] generate
Or, to show the "power of the R language", so to speak, and
assuming that
Dave is correct, there is no need to use a for() loop:
xmat <- matrix( rnorm( 27*5, rep(mu,5), rep(sig,5) ) , nrow=27)
To verify that I've created the vectors in the right order, and
converted
to a matrix in the right order, try this:
mu <- 1:27
sig <- rep(1,27)
x.matrix<-matrix(0,nrow=27,ncol=5)
for(i in 1:27){
# x.matrix[i,j]<-rnorm(5,mu[i],sig[i])
## Dave's suggestion:
x.matrix[i, ] <- rnorm(5,mu[i],sig[i]) # since 'j' doesn't
exist
}
## loop not necessary:
xmat <- matrix( rnorm( 27*5, rep(mu,5), rep(sig,5) ) , nrow=27)
plot( rowMeans(x.matrix), rowMeans(xmat))
abline(0,1)
--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
On 3/27/14 12:39 PM, "David Winsemius" <dwinsemius at comcast.net>
wrote:
>
>On Mar 27, 2014, at 4:31 AM, IZHAK shabsogh wrote:
>
>> i try to generate 27*5 matrix of observation using the
following code
>>but is given error
>> kindly assist and correct the problem
>>
>>
>>
x1<-c(-1,0,1,-1,0,1,-1,0,1,-1,0,1,-1,0,1,-1,0,1,-1,0,1,-1,0,1,-1
,0,1)
>>
x2<-c(-1,-1,-1,0,0,0,1,1,1,-1,-1,-1,0,0,0,1,1,1,-1,-1,-1,0,0,0,1
,1,1)
>>
x3<-c(-1,-1,-1,-1,-1,-1,-1,-1,-1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1
,1,1)
>> mu<-50+5*(x1^2+x2^2+x3^2)
>> sig<-100+5*((x1-0.5)^2+x2^2+x3^2)
>> x.matrix<-matrix(0,nrow=27,ncol=5)
>> for(i in 1:27){
>
>Perhaps, instead:
>
> x.matrix[i, ]<-rnorm(5,mu[i],sig[i]) # since 'j' doesn't
exist
>
>> x.matrix[i,j]<-rnorm(5,mu[i],sig[i])
>> }
>>
>>
>> Thanks
>> Ishaq
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>>http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible
code.
>
>David Winsemius
>Alameda, CA, USA
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible
code.
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible
code.
More information about the R-help
mailing list