[R] (no subject)

Adaikalavan Ramasamy ramasamy at cancer.org.uk
Thu Nov 11 20:07:33 CET 2004


Please read the posting guide that tells you to a) use a meaningful
subject line b) give a reproducible example if you can c) read the
manuals and help files first

If you have a vector (not a list) of numbers, say x, and you want to
center it on the median you can do

x <- c(1,2,3,4,5)

Then x - median(x) will give you the deviation of x from its median.
Note that operations in R is vectorised, so you do not need to to do
something like "for(i in 1:5) y[i] <- x[i] - median(x)".

Next you want to square this deviation and sum over all elements.

d <- x - median(x)
sum( d^2 )

Or you can do it in one line as  "sum( (x - median(x))^2 )"


On Thu, 2004-11-11 at 16:28, Wei Yang wrote:
> Hi, 
> 
>  
> 
> I have a list of numbers. For each of the numbers, I take sum of squares of
> the numbers centered on the number chosen. If it is less than a certain
> constant, I will take the average of the numbers chosen.  
> 
>  
> 
> Anyone can give me a sample code. You help will be greatly appreciated. 
> 
>  
> 
> Peter
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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
-- 
Adaikalavan Ramasamy                    ramasamy at cancer.org.uk
Centre for Statistics in Medicine       http://www.ihs.ox.ac.uk/csm/
Cancer Research UK                      Tel : 01865 226 677
Old Road Campus, Headington, Oxford     Fax : 01865 226 962




More information about the R-help mailing list