[R] matrix dimension and for loop

sonchawan tamkaew psu17772 at yahoo.com
Tue Apr 9 14:46:40 CEST 2002


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?

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.

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

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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list