Hi there fellow R-users, Im seeing some strange behaviour when I multiply a vector by a matrix Here is my script:> tr1 2 3 4 5 6 0.2217903 0.1560525 0.1487908 0.1671354 0.1590643 0.1471667> > ex1a 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:kssg@kssg.com http://www.kssg.com The information in this Internet email is confidential and m...{{dropped}}
What you need to realize is that R does vectorized computation in `Fortran' order. Here's a perhaps more illuminating example:> tr <- rep(1:2, 3) > tr[1] 1 2 1 2 1 2> ex1 <- as.data.frame(matrix(1:12, 2, 6)) > ex1V1 V2 V3 V4 V5 V6 1 1 3 5 7 9 11 2 2 4 6 8 10 12> tr * ex1[1,]V1 V2 V3 V4 V5 V6 1 1 6 5 14 9 22> (tr * ex1)[1,]V1 V2 V3 V4 V5 V6 1 1 3 5 7 9 11 When you do (tr * ex1)[1,], R multiplies tr and ex1 element-by-element, stacking _columns_ of ex1 to match tr, and recyle the elements of tr to match the length of the `stacked' ex1. Andy> From: Wayne Jones > > 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:kssg at kssg.com http://www.kssg.com > > > The information in this Internet email is confidential and > m...{{dropped}} > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
I think this is as intended. When you do elementwise multiplication of a vector by a matrix R there are three important points to keep in mind: 1) element-wise multiplication treats things as vectors 2) when you treat a matrix as a vector you get the _columns_ stacked one on top of another. 3) R recycles elements of the shorter vector So tr*(ex[1,]) is the same as multiplying _row_ 1 by the elements of tr, but (tr*ex)[1,] multiplies the first _column_ of ex by the first 2 elements of tr, then the second column by the second two, then the third column by the third two, then recycles for columns 4, 5 and 6. Then the [1,] takes the first row of the result, so you (tr[1] tr[3] tr[5] tr[1] tr[3] tr[5]) times the first row. Which looks like what you have. Reid Huntsinger -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Wayne Jones Sent: Monday, October 04, 2004 10:11 AM To: R help list Subject: [R] Strange Matrix Multiplication Behaviour Hi there fellow R-users, Im seeing some strange behaviour when I multiply a vector by a matrix Here is my script:> tr1 2 3 4 5 6 0.2217903 0.1560525 0.1487908 0.1671354 0.1590643 0.1471667> > ex1a 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:kssg at kssg.com http://www.kssg.com The information in this Internet email is confidential and m...{{dropped}} ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
This is *not* matrix multiplication, which uses %*% in R. Might that help explain your confusion? The difference is explained in `An Introduction to R'. On Mon, 4 Oct 2004, 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.Why? The second multiplies the elements of ex by tr, in column-major order, and what is `natural' about that? -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
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)) > ex1V1 V2 V3 V4 V5 V6 1 1 2 3 4 5 6 2 7 8 9 10 11 12> tr * ex1V1 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:kssg at kssg.com http://www.kssg.com > > > The information in this Internet email is confidential and m...{{dropped}} > > ______________________________________________ > R-help at stat.math.ethz.ch 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 ahenningsen at agric-econ.uni-kiel.de http://www.uni-kiel.de/agrarpol/ahenningsen/
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:ahenningsen@email.uni-kiel.de] Sent: 04 October 2004 16:09 To: r-help@stat.math.ethz.ch 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)) > ex1V1 V2 V3 V4 V5 V6 1 1 2 3 4 5 6 2 7 8 9 10 11 12> tr * ex1V1 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:kssg@kssg.com http://www.kssg.com > > > The information in this Internet email is confidential and m...{{dropped}} > > ______________________________________________ > R-help@stat.math.ethz.ch 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 ahenningsen@agric-econ.uni-kiel.de http://www.uni-kiel.de/agrarpol/ahenningsen/ [[alternative HTML version deleted]]
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: >"*" does array or element-wise multiplication. %*% is Matrix multiplication. In the first case, you are multiplying tr by the first row of ex1 > tr * ex1[1, ] a b c d e f 1 0.05122422 -0.5117032 -0.09961093 -0.1842568 0.03664727 -0.2285117 In the second, you are extracting the first row of the result of multiplying tr by ex1, which as we see below returns a 2x6 matrix: > tr * ex1 a b c d e f 1 0.05122422 -0.4878917 -0.10648873 -0.24451059 0.03428033 -0.24698556 2 -0.04471461 -0.4211459 -0.01675805 -0.05573933 -0.44671804 -0.04852005 > (tr * ex1)[1, ] a b c d e f 1 0.05122422 -0.4878917 -0.1064887 -0.2445106 0.03428033 -0.2469856 Note that tr gets recycled in the tr * ex1 and (tr * ex1)[1, ] as tr is not as long as ex1. Element 1b in (tr * ex1)[1, ] is formed by multiplying element 1b of ex1 (-3.279045) by the third element of the vector tr (0.1487908): > tr[3] * ex1[1,2] [1] -0.4878917 the second value of the vector tr was used to multiply against element 2a in matrix ex1: but in the tr * ex1[1, ] case tr and ex1[1, ] both contain 6 elements and the element 1b in the results is formed by multiplying element 1b of ex1[1, ] (-3.279045) by the second element of vector tr (0.1560525) > tr[2] * ex1[1,2] [1] -0.5117032> > Notice that the output from tr * ex[1,] is different from (tr* ex1)[1,] > Especially element number 4.The difference is due to recycling of tr and of where you are doing your sub setting> I would naturally expect both vectors to be equivalent. >Hopefully my longwinded explanation helps. Gav -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Gavin Simpson [T] +44 (0)20 7679 5522 ENSIS Research Fellow [F] +44 (0)20 7679 7565 ENSIS Ltd. & ECRC [E] gavin.simpson at ucl.ac.uk UCL Department of Geography [W] http://www.ucl.ac.uk/~ucfagls/cv/ 26 Bedford Way [W] http://www.ucl.ac.uk/~ucfagls/ London. WC1H 0AP. %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
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)) > ex1V1 V2 V3 V4 V5 V6 1 1 2 3 4 5 6 2 7 8 9 10 11 12> tr * ex1V1 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:kssg at kssg.com http://www.kssg.com > > > The information in this Internet email is confidential and m...{{dropped}} > > ______________________________________________ > R-help at stat.math.ethz.ch 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 ahenningsen at agric-econ.uni-kiel.de http://www.uni-kiel.de/agrarpol/ahenningsen/