[R] Equations as arguments to functions
Michaell Taylor
pols1oh at bestweb.net
Tue Nov 27 22:34:55 CET 2001
I am trying to pass an equation as an arguement to a function, which seems
pretty straightforward given lazy evaluation.
I constructed the following little test to make sure I sort of knew what I
was doing:
go <- function(X,eq) { C <<- X*2; d <<- eq}
a _ rnorm(100)
go(10,eq=a[.5*length(a)]+C+1)
> C
[1] 20
> d
[1] 21.10551
> a[.5*length(a)]
[1] 0.1055086
Everything looks good - (BTW seems like a really powerful feature of R)
Now on to my code of interest, which doesn't work (shown below).
rm(list=ls())
load("datasets/citycodes")
increments _ seq(5000,100000,by=5000)
makeindex <- function(Y,eq) {
losers _ 0; levels_0
index _ 0; location _ 0; cutoff _0; totalapt_0; houses_0
for (C in city[1:2]) {
counter _ 0
if (file.exists(paste("datasets/2000income/",C,sep=""))) {
load(paste("datasets/2000income/",C,sep=""))
counter _ counter+1}
if (file.exists(paste("datasets/rents/2000",toupper(C),sep=""))) {
load(paste("datasets/rents/2000",toupper(C),sep=""))
counter _ counter+1}
if (counter== 2) {
totrent _ 0
totrent _ rep(rent,units)
totrent _ totrent[!is.na(totrent)]
income2000 _ sort(income2000)
print(C)
print(length(income2000)) # for debugging - notice this works fine
print(ls()) # for debugging - notice this works fine
level _ eq # FAILURE POINT
print(level)
maxrent _ level/12*Y
tot _ length(totrent[totrent<=maxrent])/length(totrent)
house _ length(income2000[income2000<=level])/length(income2000)
location _ c(location,C)
index _ c(index,tot)
cutoff _ c(cutoff,maxrent)
totalapt _ c(totalapt,length(totrent))
levels _ c(levels,level)
houses _ c(houses,house)
}
if (counter<2) {losers _ cat(losers,C)}
}
overall _ data.frame(location,levels,index,houses,cutoff,totalapt)
overall <<- overall[-1,]
print(overall)
losers <<- losers[-1]
rm("overall","losers","level","maxrent")
}
makeindex(.35,eq=(income2000[.33*length(income2000)]))
*************** GIVES ********************
> source("finalindex.R")
[1] "At"
[1] 1459919
[1] "C" "counter" "cutoff" "eq" "houses"
[6] "income2000" "index" "levels" "location" "losers"
[11] "rent" "totalapt" "totrent" "units" "Y"
[16] "yr"
Error: Object "income2000" not found
Ummm. I see it in the list which immediately preceded it.
Please notice that I put a ls() in this function just prior to the calling of
eq so as to verify that income2000 is indeed "found".
while,
makeindex(.35,eq=(20000*.9))
works fine.
This smells like a "I am an idiot" sort of problem. I must be missing
something simple. Thanks for any help - BTW sorry for the long post, trying
to give sufficient data.
=========================================
Michaell Taylor, PhD
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list