[R] Function to read a string as the variables as opposed totaking the string name as the variable

Bert Gunter gunter.berton at gene.com
Thu May 14 23:37:37 CEST 2009


 As Hadley said, it's complex in general: the standard approach is to
assemble the arguments into a list and then use do.call -- ?do.call.
Undoubtedly, there are times where this won't do either (at least not
without some further tricks).

-- Bert

Bert Gunter
Genentech Nonclinical Biostatistics


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of hadley wickham
Sent: Thursday, May 14, 2009 11:41 AM
To: Lori Simpson
Cc: r-help at r-project.org; Max Rausch
Subject: Re: [R] Function to read a string as the variables as opposed
totaking the string name as the variable

On Thu, May 14, 2009 at 12:16 PM, Lori Simpson
<lori.simpson at dc-energy.com> wrote:
> I am writing a custom function that uses an R-function from the
> reshape package: cast.  However, my question could be applicable to
> any R function.
>
> Normally one writes the arguments directly into a function, e.g.:
>
> result=cast(table1, column1 + column2 + column3   ~    column4,
> mean)      (1)
>
> I need to be able to write this statement as follows:
>
> result=cast(table1, string_with_columns   ~    column4, mean)    (2)
> string_with_columns = group of functions that ultimately outputs:
> "column1 + column2 + column3"

It's complex in general, but for cast you can just supply a string:

cast(table, paste(string_with_columns, "~ column4"))

Hadley

-- 
http://had.co.nz/

______________________________________________
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.




More information about the R-help mailing list