Hello dear R people! Several times it occurred to me that a function that uses a variable name as a parameter would be helpful, but I did not find out how to write such a function. I have experience in several programming language, but I did not come across a helpful trick... What I want to have is... a <- 12 # starting value add <- function(variable,increment) { variable <- variable + increment } # by typing a and 25 for example, you specify that 25 will be added to the variable a... add(a,25) # the new a equals 12 + 25 = 37 Thanks for all the help ? I'll try to give my advice when I get across a problem I can solve! David -- GMX Download-Spiele: Preizsturz! Alle Puzzle-Spiele Deluxe ?ber 60% billiger. http://games.entertainment.gmx.net/de/entertainment/games/download/puzzle/index.html
You're almost there: a <- 12 # starting value add <- function(variable,increment) { variable + increment } add(a,25) you shouldn't assign variable + increment to variable, unless you put a return in your function: add <- function(variable,increment) { variable <- variable + increment return(variable) } Bart David Croll wrote:> > > > Hello dear R people! > > > Several times it occurred to me that a function that uses a variable name > as a parameter would be helpful, but I did not find out how to write such > a function. I have experience in several programming language, but I did > not come across a helpful trick... > > What I want to have is... > > a <- 12 # starting value > > add <- function(variable,increment) { > > variable <- variable + increment > > } > > # by typing a and 25 for example, you specify that 25 will be added to the > variable a... > > add(a,25) > > # the new a equals 12 + 25 = 37 > > Thanks for all the help ? I'll try to give my advice when I get across a > problem I can solve! > > > David > -- > GMX Download-Spiele: Preizsturz! Alle Puzzle-Spiele Deluxe ?ber 60% > billiger. > http://games.entertainment.gmx.net/de/entertainment/games/download/puzzle/index.html > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- View this message in context: http://www.nabble.com/function-that-uses-a-variable-name-as-the-parameter-tp20459842p20460244.html Sent from the R help mailing list archive at Nabble.com.
Wacek Kusnierczyk
2008-Nov-12 13:28 UTC
[R] function that uses a variable name as the parameter
David Croll wrote:> Hello dear R people! > > > Several times it occurred to me that a function that uses a variable name as a parameter would be helpful, but I did not find out how to write such a function. I have experience in several programming language, but I did not come across a helpful trick... > > What I want to have is... > > a <- 12 # starting value > > add <- function(variable,increment) { > > variable <- variable + increment > > } > > # by typing a and 25 for example, you specify that 25 will be added to the variable a... > > add(a,25) > > # the new a equals 12 + 25 = 37 > >add = function(variable, value) assign(deparse(substitute(variable)), variable + value, envir=parent.frame()) a = 1 add(a, 2) a # 3 addtoa = function(value) add(a, value) addtoa(10) a # still 3 add = function(variable, value) assign(deparse(substitute(variable)), variable + value, inherits=TRUE) addtoa(10) a # 13 ?assign vQ
Duncan Murdoch
2008-Nov-12 13:30 UTC
[R] function that uses a variable name as the parameter
On 12/11/2008 7:59 AM, David Croll wrote:> > Hello dear R people! > > > Several times it occurred to me that a function that uses a variable name as a parameter would be helpful, but I did not find out how to write such a function. I have experience in several programming language, but I did not come across a helpful trick... > > What I want to have is... > > a <- 12 # starting value > > add <- function(variable,increment) { > > variable <- variable + increment > > } > > # by typing a and 25 for example, you specify that 25 will be added to the variable a... > > add(a,25) > > # the new a equals 12 + 25 = 37 > > Thanks for all the help ? I'll try to give my advice when I get across a problem I can solve!That's not a good idea: it looks as though you're trying to fake "call by reference" in a call by value language. R is flexible enough to do it (use substitute() to change the arg into a string, get() to get the old value, assign() to put it back), but the behaviour will be really weird, and you'll probably run into problems later. R is not C. Duncan Murdoch
David, Having a function that changes global variables is very frowned on with few exceptions. It falls into the category of things that you should never do unless you fully understand why you should never do them (also see fortune(36)). However, what you want to do may be more of a macro than a function, see Thomas Lumley's article on macros in the September 2001 newsletter for some detail and the defmacro function in the gtools package to see if they fill your need. -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of David Croll > Sent: Wednesday, November 12, 2008 5:59 AM > To: r-help at r-project.org > Subject: [R] function that uses a variable name as the parameter > > > > Hello dear R people! > > > Several times it occurred to me that a function that uses a variable > name as a parameter would be helpful, but I did not find out how to > write such a function. I have experience in several programming > language, but I did not come across a helpful trick... > > > What I want to have is... > > a <- 12 # starting value > > add <- function(variable,increment) { > > variable <- variable + increment > > } > > # by typing a and 25 for example, you specify that 25 will be added to > the variable a... > > add(a,25) > > # the new a equals 12 + 25 = 37 > > Thanks for all the help ? I'll try to give my advice when I get across > a problem I can solve! > > > David > -- > GMX Download-Spiele: Preizsturz! Alle Puzzle-Spiele Deluxe ?ber 60% > billiger. > http://games.entertainment.gmx.net/de/entertainment/games/download/puzz > le/index.html > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.
William Dunlap
2008-Nov-13 18:51 UTC
[R] function that uses a variable name as the parameter
On 12/11/2008 7:59 AM, David Croll wrote:> > Hello dear R people! > > > Several times it occurred to me that a function that uses a variablename as a parameter would be helpful, but I did not find out how to write such a function. I have experience in several programming language, but I did not come across a helpful trick...> > What I want to have is... > a <- 12 # starting value > add <- function(variable,increment) { > variable <- variable + increment > } > > # by typing a and 25 for example, you specify that 25 will be added tothe variable a...> add(a,25) > # the new a equals 12 + 25 = 37 > > Thanks for all the help - I'll try to give my advice when I get acrossa problem I can solve! To do this with normal looking S syntax and semantics (no dynamic scoping) you could use a 'replacement function' (is there a better standard term for that?). E.g., `incrementBy<-` <- function(x, value) { x <- x + value x } Use it as > a<-17 > incrementBy(a) <- 25 > a [1] 42 Note how it is clear from the usage 'a' is being changed, since it is on the left side of the assignment. Usually replacement functions have a non-replacement analog which does roughly the inverse of the replacement, but I'm not sure if that would make sense here. You could write a log<- function that is the inverse of the log function `log<-` <- function(x, base = exp(1), value) { x[] <- base ^ value x } and combine it with incrementBy to increment on a log scale. E.g. > x<-66:68 > incrementBy(log(x,base=10)) <- 2 # increment log(x,10) by 2 => multiply x by 100 > x [1] 6600 6700 6800 Bill Dunlap TIBCO Spotfire Inc wdunlap tibco.com