Reputation: 180
I am trying to replace attach() with with() in a function. I've created a simple example below that mimics the problem. I'd like to be able to use a particularly variable from a data set, and also to be able to modify that variable (e.g., squaring or dividing by 1000 as below).
dummy.data=data.frame(CVD=1:10,pop=1:10)
# this works
working=function(data,offset){
attach(data)
b=offset
print(summary(b))
detach(data)
}
working(dummy.data,pop/1000)
# this does not work
not.working=function(data,offset){
with(data,b=offset)
print(summary(b))
}
not.working(dummy.data,pop/1000)
Upvotes: 0
Views: 94
Reputation: 263342
This does the same operation, but the goals of this exercise remain opaque to me:
now.working=function(data,offset){
b=offset; cat(str(b))
with(data, print(summary(eval(b))))
}
now.working(dummy.data,expression(pop/1000) )
Upvotes: 1