Skip to content

scoping issues?

4 messages · Tom, Jan T. Kim, Barry Rowlingson

Tom
#
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
Browse[1]> mean(amps[1],amps[2],amps[3],amps[7],amps[8])
[1] 1
Tom
#
Thanks for the answers, yup the missing c() was what was throwing me.
And thanks Barry for the slighly more elegant code, I'm a bit post
christmas party here and not thinking as straight as I should be.
On Thu, 2005-08-12 at 06:47 -0500, tom wright wrote:
#
On Thu, Dec 08, 2005 at 06:47:05AM -0500, tom wright wrote:
This computes the mean of the set {1, 1, 2, 2, 1}, which is 7 / 5 = 1.4
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
#
tom wright wrote:
> Browse[1]> mean(amps[1],amps[2],amps[3],amps[7],amps[8])
 > [1] 1

  For starters, this just returns mean(amps[1]). 'mean' computes the 
mean of the first argument, the others are slurped up by '...' and in 
this case thrown into the bin. You want to do 
mean(c(amps[1],amps[2],amps[3] and so on. Wrap them into a single vector.

  For main course, I think you can do it like this:

getAR <- function(v.amps){
   sv=sort(v.amps)
   sum(sv[1:3])/mean(sv[-(1:3)])
}

  - which computes the ratio of the sum of the three biggest over the 
mean of the remainder. Which I think is what your code looks like its 
trying to do!

  An example input/output would be nice. My code gives:

  > amps<-c(1,2,3,3,3,2,1)
  > getAR(amps)
  [1] 1.454545

  but I still dont know if that's what it should be!

  For dessert, I don't think its a scoping issue, I think you've not 
really explained what the problem is...

Baz