[R] how to use a column name from the data frame in the function

Steve Lianoglou lianoglou.steve at gene.com
Fri Aug 23 07:48:48 CEST 2013


Hi,

On Thu, Aug 22, 2013 at 9:49 PM, Jeff Newmiller
<jdnewmil at dcn.davis.ca.us> wrote:
> Please don't post in HTML format... it messes with code examples.
>
> Use character indexing (please read the Introduction to R... again if necessary).
>
> myf <- function(df, colname){
>   df[ ,colname ]
> }

Or df[[colname]] for data.frames

> colname  <- "a"
> myf(m,colname)
>
> Until you learn simple R syntax, I strongly recommend avoiding writing tricky code that plays with names of variables.

And even after you learn simple R syntax, if you think the right thing
to do is to use some combo of substitute/eval/quote and friends, I'd
strongly encourage you to think again ...

-steve

-- 
Steve Lianoglou
Computational Biologist
Bioinformatics and Computational Biology
Genentech



More information about the R-help mailing list