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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On 04/09/02 05:46, sonchawan tamkaew wrote:>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?x <- matrix(rnorm(50),10,5) # collapsing your first two commands y <- 1:5 Z <- matrix(0,nrow(x),length(y)) # case sensitive, and y is a vector Z[,length(y)] <- x %*% y It isn't clear that this is what you want, though. It fills up one column of Z.>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.I think it will still work. So long as you pay attention to what is a matrix - to which dim() applies - and what is a vector, to which length() applies. -- Jonathan Baron, Professor of Psychology, University of Pennsylvania Home page: http://www.sas.upenn.edu/~baron -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
sonchawan tamkaew <psu17772 at yahoo.com> writes:> 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?Yes. If you multiply (i.e. the '*' operator) a matrix and a vector you will do operations columnwise on the matrix. Here you want to multiply the rows so you could transpose x, multiply by y and transpose the result.> x <- array(rnorm(50), dim = c(10,5)) > dim(x)[1] 10 5> y <- 1:5 > Z <- t(y * t(x)) > dim(Z)[1] 10 5> x[1,][1] 0.23144399 -0.25393369 0.06469486 -0.13116405 0.48534804> Z[1,][1] 0.2314440 -0.5078674 0.1940846 -0.5246562 2.4267402> 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.The above will work in that case too.> x <- rnorm(10) > y <- 2 > t(y * t(x))[,1] [1,] 1.5755705 [2,] 0.3811325 [3,] -0.4629773 [4,] 1.7612785 [5,] 0.7881174 [6,] -0.2815202 [7,] 0.4315893 [8,] 1.1609736 [9,] 1.8737894 [10,] 5.3069982 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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? > > 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.x %*% diag(y, ncol(x)) Uwe Ligges -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On 9 Apr 2002 at 5:46, 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 <- t( t(x) * y )> > 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 dimensionsThe answer already given to theprevious question automatically handles the case where x is a vector so its not necessary to provide special code for it. However, even though its not needed here, just for interest: x <- as.matrix( x ) will turn vector x into a 1 column matrix and leave it as is if its already a matrix. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> 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 commandI thought I would collect together some of the solutions that have been posted as well as mention a few others. I have also added some discussion particularly with reference to the second question. There appears to be a myriad of ways to address this problem in R. Since I am learning R I found it useful for myself to explore various solutions and maybe others will find this helpful too. The problem is to multiply the jth column of a matrix x by y[j] for each j. Its also possible for x to be a vector in which case y may be a scalar. The first 4 solutions regard x as a one column matrix if x is a vector and so always return a matrix. The 5th solution returns a vector if x is a vector. 1. Matrix multiplication. Note that if x is a vector then it is treated as a one column matrix. Also note that NROW(x) is the same as nrow(x), if x is a matrix; however, if x is a vector, then NROW(x) is its length while nrow(x) is NULL. Thus it is important here to use NROW and not nrow. This is discussed in the help on nrow. x %*% diag(y,NROW(x)) 2. Scalar multiplication and transpose. Note that if x is a vector then t(x) is a row matrix made out of it, so no special processing is necessary. t(t(x)*y) 3. Sweep. Note that as.matrix(x) is the same as x, if x is a matrix, but is a column matrix made out of x, if x is a vector. sweep(as.matrix(x),2,y,FUN="*") 4. Scalar multiplication and col. col(m) is a matrix the same size as matrix m. Every entry in column j equals j (for all columns). as.matrix(x) * y[col(as.matrix(x))] 5. Variant of #4. The following is the same as #4 except that it returns a vector if x is a vector. x * y[col(as.matrix(x))] as.matrix(x) can be abbreviated to just x, in #3, #4 and #5 if x is known always to be a matrix. For example, the last two solutions would be just x*y[col(x)]. I had not expected the last 2 solutions to work in the case that x is a vector and y is a scalar, since they involve subscripting of a scalar, but apparently that works in R. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._