Re: [R] scoping issues?

From: Jan T. Kim <jtk_at_cmp.uea.ac.uk>
Date: Fri 09 Dec 2005 - 05:14:09 EST

On Thu, Dec 08, 2005 at 06:47:05AM -0500, tom wright wrote:
> Can anyone please help me understand whats happening here?
> Thanks
> Tom
>
> getAmpRatio<-function(v_amps){
> #calculates the amplitude ratios between the 3 largest amps and the
> rest
> bigamp<-0
> map<-rep(TRUE,length(v_amps))
>
> for(iLoc in 1:3){
> bigamp<-bigamp+max(v_amps)
> map[which.max(v_amps)]<-FALSE
> v_amps<-v_amps[map]
> map<-rep(TRUE,length(v_amps))
> }
> browser()
> return(bigamp/mean(v_amps))
> }
>
> amps<-c(1,2,3,3,3,2,1)
> getAmpRatio(amps)
>
> Browse[1]> v_amps
> [1] 1 1 2 2 1
> Browse[1]> c(amps[1],amps[2],amps[3],amps[7],amps[8])
> [1] 1 1 2 2 1
> Browse[1]> mean(v_amps)
> [1] 1.4

This computes the mean of the set {1, 1, 2, 2, 1}, which is 7 / 5 = 1.4

> Browse[1]> mean(amps[1],amps[2],amps[3],amps[7],amps[8])
> [1] 1

This computes the mean of amps[1], i.e. of the set {1}, which is 1 / 1 = 1.

The remaining parameters are matched to the special variable length formal parameter of the mean function, which, upon eventual dispatch to mean.default are basically ignored, as far as I see.

Perhaps, you meant

    mean(c(amps[1], amps[2], amps[3], amps[7], amps[8]))

which effectively calls mean with one argument of length 5, as opposed to 5 arguments of length 1, as your call does.

Best regards, Jan

-- 
 +- Jan T. Kim -------------------------------------------------------+
 |             email: jtk@cmp.uea.ac.uk                               |
 |             WWW:   http://www.cmp.uea.ac.uk/people/jtk             |
 *-----=<  hierarchical systems are for files, not for humans  >=-----*

______________________________________________
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
Received on Fri Dec 09 06:07:45 2005

This archive was generated by hypermail 2.1.8 : Fri 09 Dec 2005 - 09:31:31 EST