[R] Symbolic references - passing variable names into functions

Erik Iverson eiverson at NMDP.ORG
Wed Aug 12 16:34:21 CEST 2009

I think ONE answer to what you actually want to do might be

f <- function(dataf, col1 = "column1", col2 = "column2") {
        dataf[[col1]] <- dataf[[col2]] # just as an example

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Alexander Shenkin
Sent: Wednesday, August 12, 2009 9:27 AM
To: r-help at r-project.org
Subject: [R] Symbolic references - passing variable names into functions

Hello All,

I am trying to write a function which would operate on columns of a
dataframe specified in parameters passed to that function.

    f = function(dataf, col1 = "column1", col2 = "column2") {
        dataf$col1 = dataf$col2 # just as an example

The above, of course, does not work as intended.  In some languages one
can force evaluation of a variable, and then use that evaluation as the
variable name.  Thus,

    > a = "myvar"
    > (operator)a = 1
    > myvar
    [1] 1

Is there some operator which allows this symbolic referencing in R?


R-help at r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list