Skip to content
Back to formatted view

Raw Message

Message-ID: <CF9BAE96C1AB4D98BFC53F900C63B46F@gne.windows.gene.com>
Date: 2009-05-14T21:37:37Z
From: Bert Gunter
Subject: Function to read a string as the variables as opposed totaking the string name as the variable
In-Reply-To: <f8e6ff050905141141n2f863df0pa245d18f02508833@mail.gmail.com>

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.