[R] R WRONG CALCULATIONS - Please Help

Rui Barradas ruipbarradas at sapo.pt
Mon Sep 14 19:04:23 CEST 2015


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



More information about the R-help mailing list