[R] matrix dimension and for loop
Laurent Gautier
laurent at genome.cbs.dtu.dk
Tue Apr 9 19:06:01 CEST 2002
On Tue, Apr 09, 2002 at 05:46:40AM -0700, sonchawan tamkaew wrote:
> Dear all,
>
> My questions are that if I have
>
> > x<-rnorm(50)
> > dim(x)<-c(10,5)
> > y=1:5
> > Z<-matrix(0,NROW(x),NROW(y))
> > for (j in 1:NROW(y)) Z[,j]<-x[,j]*y[j]
>
> 1. Is there any other way to write this without 'for'
> loop?
Z <- x * y
seems to do the job....
>
> 2. and if I don't know the dimension of x, which could
> be only 1 column, how could I write the command
> without using if (NCOL(x)==1), or something like that?
> (in case I want to include them in my function.
>
still seems to do the job...
> Example of x is a vector.
> > x<-rnorm(10)
> > y=2
> > Z<-matrix(0,NROW(x),NROW(y))
> > for (j in 1:NROW(y)) Z[,j]<-x[,j]*y[j]
> Error in x[, j] : incorrect number of dimensions
>
note: I discovered above that assignment like 'y=2' where now valid....
(learn something new evryday on r-help.... ;-) )
where now valid....
(learn something new evryday on r-help.... ;-) )
L
> Could anyone please help me to solve these problem?
> Thank you very much for your kindness.
>
>
> Sonchawan Tamkaew
>
> __________________________________________________
>
>
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
--
--------------------------------------------------------------
other email: lgautier at altern.org
--------------------------------------------------------------
Laurent Gautier CBS, Building 208, DTU
PhD. Student D-2800 Lyngby,Denmark
tel: +45 45 25 24 85 http://www.cbs.dtu.dk/laurent
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list