Thanks for your help folks. I was not aware of the column wise nature of R multiplication.
regards Wayne -----Original Message----- From: Arne Henningsen [mailto:[EMAIL PROTECTED] Sent: 04 October 2004 16:09 To: [EMAIL PROTECTED] Cc: Wayne Jones Subject: Re: [R] Strange Matrix Multiplication Behaviour Hi Wayne, are you aware that you are NOT doing matrix multiplication? A matrix multiplication is done by %*%, e.g. > tr %*% t(as.matrix(ex1) Or do you want a normal scalar multiplication with the elements of each row of ex1 to be multiplied with the corresponding element of tr? However, R does the multiplication not row-wise, but column-wise. Thus, the results are not as you expected. To do a row-wise multiplication you can transform the data.frame: > tr <- as.vector(10*c(1:6)) > tr [1] 10 20 30 40 50 60 > ex1 <- as.data.frame(matrix(1:12,nrow=2,byrow=TRUE)) > ex1 V1 V2 V3 V4 V5 V6 1 1 2 3 4 5 6 2 7 8 9 10 11 12 > tr * ex1 V1 V2 V3 V4 V5 V6 1 10 60 150 40 150 300 2 140 320 540 200 440 720 > t( tr * t(ex1) ) V1 V2 V3 V4 V5 V6 1 10 40 90 160 250 360 2 70 160 270 400 550 720 > t( tr * t(ex1) )[1,] V1 V2 V3 V4 V5 V6 10 40 90 160 250 360 > t( tr * t(ex1[1,]) ) V1 V2 V3 V4 V5 V6 1 10 40 90 160 250 360 I hope that this makes it clear. Best wishes, Arne On Monday 04 October 2004 16:10, Wayne Jones wrote: > Hi there fellow R-users, > > Im seeing some strange behaviour when I multiply a vector by a matrix > > Here is my script: > > tr > > 1 2 3 4 5 6 > 0.2217903 0.1560525 0.1487908 0.1671354 0.1590643 0.1471667 > > > ex1 > > a b c d e f > 1 0.2309579 -3.279045 -0.6694697 -1.1024404 0.2303928 -1.5527404 > 2 -0.2865357 -2.519789 -0.1138712 -0.3571832 -2.6727913 -0.3296945 > > > is.vector(tr) > > [1] TRUE > > > is.data.frame(ex1) > > [1] TRUE > > > tr* ex1[1,] > > a b c d e f > 1 0.05122423 -0.5117031 -0.09961092 -0.1842569 0.03664727 -0.2285117 > > > (tr* ex1)[1,] > > a b c d e f > 1 0.05122423 -0.4878917 -0.1064887 -0.2445106 0.03428033 -0.2469855 > > > Notice that the output from tr * ex[1,] is different from (tr* ex1)[1,] > Especially element number 4. > > I would naturally expect both vectors to be equivalent. > > > Can anyone shed any light on my predicament?? > > > version > > _ > platform i386-pc-mingw32 > arch i386 > os mingw32 > system i386, mingw32 > status > major 1 > minor 9.1 > year 2004 > month 06 > day 21 > language R > > > > Regards > > Wayne Jones > > > > > > > KSS Ltd > Seventh Floor St James's Buildings 79 Oxford Street Manchester M1 6SS > England Company Registration Number 2800886 > Tel: +44 (0) 161 228 0040 Fax: +44 (0) 161 236 6305 > mailto:[EMAIL PROTECTED] http://www.kssg.com > > > The information in this Internet email is confidential and m...{{dropped}} > > ______________________________________________ > [EMAIL PROTECTED] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html -- Arne Henningsen Department of Agricultural Economics University of Kiel Olshausenstr. 40 D-24098 Kiel (Germany) Tel: +49-431-880 4445 Fax: +49-431-880 1397 [EMAIL PROTECTED] http://www.uni-kiel.de/agrarpol/ahenningsen/ [[alternative HTML version deleted]] ______________________________________________ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html