I would greatly appreciate if you could let me know why the R does not make the right computations in the case below. Waiting for your reply Jorge Cola?o R version 3.2.2 (2015-08-14) -- "Fire Safety" Copyright (C) 2015 The R Foundation for Statistical Computing Platform: i386-w64-mingw32/i386 (32-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R.> X<-matrix(c(-1,0,1,-1,1,0,+ 0,0,1,1,1,-1, + -1,0,-1,1,0,1, + 1,1,-1,-1,0,0, + 0,0,1,1,-1,1),nrow=5,ncol=6,byrow=T)>mean<-c(mean(X[,1]),mean(X[,2]),mean(X[,3]),mean(X[,4]),mean(X[,5]),mean(X[,6]))> mean[1] -0.2 0.2 0.2 0.2 0.2 0.2> X-mean[,1] [,2] [,3] [,4] [,5] [,6] [1,] -0.8 -0.2 0.8 -1.2 0.8 -0.2 [2,] -0.2 0.2 0.8 0.8 0.8 -1.2 [3,] -1.2 -0.2 -0.8 0.8 -0.2 0.8 [4,] 0.8 0.8 -1.2 -0.8 -0.2 -0.2 [5,] -0.2 -0.2 0.8 0.8 -0.8 0.8>Right Result Should Be: ans -0.80000 -0.20000 0.80000 -1.20000 0.80000 -0.20000 0.20000 -0.20000 0.80000 0.80000 0.80000 -1.20000 -0.80000 -0.20000 -1.20000 0.80000 -0.20000 0.80000 1.20000 0.80000 -1.20000 -1.20000 -0.20000 -0.20000 0.20000 -0.20000 0.80000 0.80000 -1.20000 0.80000 [[alternative HTML version deleted]]
Hello, R subtracts the _column_ vector 'mean' to X (column by column). Apparently you want the transpose, or think 'mean' is a row vector. Try instead t(t(X) - mean) And use another name for 'mean', it already is the name of a function. Hope it helps, Rui Barradas Em 14-09-2015 16:11, JORGE COLACO escreveu:> I would greatly appreciate if you could let me know why the R does not make > the right computations in the case below. > Waiting for your reply > Jorge Cola?o > > > > > > > R version 3.2.2 (2015-08-14) -- "Fire Safety" > Copyright (C) 2015 The R Foundation for Statistical Computing > Platform: i386-w64-mingw32/i386 (32-bit) > > R is free software and comes with ABSOLUTELY NO WARRANTY. > You are welcome to redistribute it under certain conditions. > Type 'license()' or 'licence()' for distribution details. > > R is a collaborative project with many contributors. > Type 'contributors()' for more information and > 'citation()' on how to cite R or R packages in publications. > > Type 'demo()' for some demos, 'help()' for on-line help, or > 'help.start()' for an HTML browser interface to help. > Type 'q()' to quit R. >> X<-matrix(c(-1,0,1,-1,1,0, > + 0,0,1,1,1,-1, > + -1,0,-1,1,0,1, > + 1,1,-1,-1,0,0, > + 0,0,1,1,-1,1),nrow=5,ncol=6,byrow=T) > >> > mean<-c(mean(X[,1]),mean(X[,2]),mean(X[,3]),mean(X[,4]),mean(X[,5]),mean(X[,6])) >> mean > [1] -0.2 0.2 0.2 0.2 0.2 0.2 >> X-mean > [,1] [,2] [,3] [,4] [,5] [,6] > [1,] -0.8 -0.2 0.8 -1.2 0.8 -0.2 > [2,] -0.2 0.2 0.8 0.8 0.8 -1.2 > [3,] -1.2 -0.2 -0.8 0.8 -0.2 0.8 > [4,] 0.8 0.8 -1.2 -0.8 -0.2 -0.2 > [5,] -0.2 -0.2 0.8 0.8 -0.8 0.8 >> > > Right Result Should Be: > > ans > -0.80000 -0.20000 0.80000 -1.20000 0.80000 -0.20000 > 0.20000 -0.20000 0.80000 0.80000 0.80000 -1.20000 > -0.80000 -0.20000 -1.20000 0.80000 -0.20000 0.80000 > 1.20000 0.80000 -1.20000 -1.20000 -0.20000 -0.20000 > 0.20000 -0.20000 0.80000 0.80000 -1.20000 0.80000 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
On Mon, Sep 14, 2015 at 11:11 AM, JORGE COLACO <j_colaco at utad.pt> wrote:> I would greatly appreciate if you could let me know why the R does not make > the right computations in the case below. > Waiting for your reply > Jorge Cola?oR made the correct computations: it did exactly what you told it. It isn't R's fault that what you told it isn't what you meant. You want to subtract the column means from each column; what you actual told R was to subtract Xmean from X element by element column-wise, recycling Xmean as necessary. Here's what you meant: X<-matrix(c(-1,0,1,-1,1,0, 0,0,1,1,1,-1, -1,0,-1,1,0,1, 1,1,-1,-1,0,0, 0,0,1,1,-1,1),nrow=5,ncol=6,byrow=T) Xmean <- colMeans(X) sweep(X, 2, Xmean, "-") Thank you for providing a simple reproducible example and clear idea of what you intended. It made answering your question very straightforward.> > R version 3.2.2 (2015-08-14) -- "Fire Safety" > Copyright (C) 2015 The R Foundation for Statistical Computing > Platform: i386-w64-mingw32/i386 (32-bit) > > R is free software and comes with ABSOLUTELY NO WARRANTY. > You are welcome to redistribute it under certain conditions. > Type 'license()' or 'licence()' for distribution details. > > R is a collaborative project with many contributors. > Type 'contributors()' for more information and > 'citation()' on how to cite R or R packages in publications. > > Type 'demo()' for some demos, 'help()' for on-line help, or > 'help.start()' for an HTML browser interface to help. > Type 'q()' to quit R. >> X<-matrix(c(-1,0,1,-1,1,0, > + 0,0,1,1,1,-1, > + -1,0,-1,1,0,1, > + 1,1,-1,-1,0,0, > + 0,0,1,1,-1,1),nrow=5,ncol=6,byrow=T) > >> > mean<-c(mean(X[,1]),mean(X[,2]),mean(X[,3]),mean(X[,4]),mean(X[,5]),mean(X[,6])) >> mean > [1] -0.2 0.2 0.2 0.2 0.2 0.2 >> X-mean > [,1] [,2] [,3] [,4] [,5] [,6] > [1,] -0.8 -0.2 0.8 -1.2 0.8 -0.2 > [2,] -0.2 0.2 0.8 0.8 0.8 -1.2 > [3,] -1.2 -0.2 -0.8 0.8 -0.2 0.8 > [4,] 0.8 0.8 -1.2 -0.8 -0.2 -0.2 > [5,] -0.2 -0.2 0.8 0.8 -0.8 0.8 >> > > Right Result Should Be: > > ans > -0.80000 -0.20000 0.80000 -1.20000 0.80000 -0.20000 > 0.20000 -0.20000 0.80000 0.80000 0.80000 -1.20000 > -0.80000 -0.20000 -1.20000 0.80000 -0.20000 0.80000 > 1.20000 0.80000 -1.20000 -1.20000 -0.20000 -0.20000 > 0.20000 -0.20000 0.80000 0.80000 -1.20000 0.80000 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Sarah Goslee http://www.functionaldiversity.org
Another option is apply(X,2,function(x) x-mean(x)) Hope this is helpful, Dan Daniel Nordlund, PhD Research and Data Analysis Division Services & Enterprise Support Administration Washington State Department of Social and Health Services -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Sarah Goslee Sent: Monday, September 14, 2015 10:07 AM To: JORGE COLACO Cc: r-help Subject: Re: [R] R WRONG CALCULATIONS - Please Help On Mon, Sep 14, 2015 at 11:11 AM, JORGE COLACO <j_colaco at utad.pt> wrote:> I would greatly appreciate if you could let me know why the R does not > make the right computations in the case below. > Waiting for your reply > Jorge Cola?oR made the correct computations: it did exactly what you told it. It isn't R's fault that what you told it isn't what you meant. You want to subtract the column means from each column; what you actual told R was to subtract Xmean from X element by element column-wise, recycling Xmean as necessary. Here's what you meant: X<-matrix(c(-1,0,1,-1,1,0, 0,0,1,1,1,-1, -1,0,-1,1,0,1, 1,1,-1,-1,0,0, 0,0,1,1,-1,1),nrow=5,ncol=6,byrow=T) Xmean <- colMeans(X) sweep(X, 2, Xmean, "-") Thank you for providing a simple reproducible example and clear idea of what you intended. It made answering your question very straightforward.> > R version 3.2.2 (2015-08-14) -- "Fire Safety" > Copyright (C) 2015 The R Foundation for Statistical Computing > Platform: i386-w64-mingw32/i386 (32-bit) > > R is free software and comes with ABSOLUTELY NO WARRANTY. > You are welcome to redistribute it under certain conditions. > Type 'license()' or 'licence()' for distribution details. > > R is a collaborative project with many contributors. > Type 'contributors()' for more information and 'citation()' on how to > cite R or R packages in publications. > > Type 'demo()' for some demos, 'help()' for on-line help, or > 'help.start()' for an HTML browser interface to help. > Type 'q()' to quit R. >> X<-matrix(c(-1,0,1,-1,1,0, > + 0,0,1,1,1,-1, > + -1,0,-1,1,0,1, > + 1,1,-1,-1,0,0, > + 0,0,1,1,-1,1),nrow=5,ncol=6,byrow=T) > >> > mean<-c(mean(X[,1]),mean(X[,2]),mean(X[,3]),mean(X[,4]),mean(X[,5]),me > an(X[,6])) >> mean > [1] -0.2 0.2 0.2 0.2 0.2 0.2 >> X-mean > [,1] [,2] [,3] [,4] [,5] [,6] > [1,] -0.8 -0.2 0.8 -1.2 0.8 -0.2 > [2,] -0.2 0.2 0.8 0.8 0.8 -1.2 > [3,] -1.2 -0.2 -0.8 0.8 -0.2 0.8 > [4,] 0.8 0.8 -1.2 -0.8 -0.2 -0.2 > [5,] -0.2 -0.2 0.8 0.8 -0.8 0.8 >> > > Right Result Should Be: > > ans > -0.80000 -0.20000 0.80000 -1.20000 0.80000 -0.20000 > 0.20000 -0.20000 0.80000 0.80000 0.80000 -1.20000 > -0.80000 -0.20000 -1.20000 0.80000 -0.20000 0.80000 > 1.20000 0.80000 -1.20000 -1.20000 -0.20000 -0.20000 > 0.20000 -0.20000 0.80000 0.80000 -1.20000 0.80000 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Sarah Goslee http://www.functionaldiversity.org ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
On Mon, Sep 14, 2015 at 04:11:57PM +0100, JORGE COLACO wrote:> > X-mean > [,1] [,2] [,3] [,4] [,5] [,6] > [1,] -0.8 -0.2 0.8 -1.2 0.8 -0.2 > [2,] -0.2 0.2 0.8 0.8 0.8 -1.2 > [3,] -1.2 -0.2 -0.8 0.8 -0.2 0.8 > [4,] 0.8 0.8 -1.2 -0.8 -0.2 -0.2 > [5,] -0.2 -0.2 0.8 0.8 -0.8 0.8 > >try this: X - mean[ col( X)] [,1] [,2] [,3] [,4] [,5] [,6] [1,] -0.8 -0.2 0.8 -1.2 0.8 -0.2 [2,] 0.2 -0.2 0.8 0.8 0.8 -1.2 [3,] -0.8 -0.2 -1.2 0.8 -0.2 0.8 [4,] 1.2 0.8 -1.2 -1.2 -0.2 -0.2 [5,] 0.2 -0.2 0.8 0.8 -1.2 0.8 Regards.> Right Result Should Be: > > ans > -0.80000 -0.20000 0.80000 -1.20000 0.80000 -0.20000 > 0.20000 -0.20000 0.80000 0.80000 0.80000 -1.20000 > -0.80000 -0.20000 -1.20000 0.80000 -0.20000 0.80000 > 1.20000 0.80000 -1.20000 -1.20000 -0.20000 -0.20000 > 0.20000 -0.20000 0.80000 0.80000 -1.20000 0.80000 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
On 15/09/15 03:11, JORGE COLACO wrote:> I would greatly appreciate if you could let me know why the R does not make > the right computations in the case below. > Waiting for your reply.It is very irritating, at least to me, when list correspondents submit postings asserting that "R gets it wrong". It is very rare for (base) R to get it wrong; there are very few bugs in base R. There are more in contributed packages, but even these are quite rare. Almost always if you think that "R gets it wrong" there is something wrong with *your* thinking or with your understanding of how R works and you need to study some more about the issue in question. Almost always the fault lies not in R but in yourself. cheers, Rolf Turner -- Technical Editor ANZJS Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276