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

hadley wickham h.wickham at gmail.com
Thu May 14 20:41:05 CEST 2009


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/




More information about the R-help mailing list