[R] string substitution for argument in function

R. Michael Weylandt michael.weylandt at gmail.com
Mon Mar 26 19:42:10 CEST 2012


'elem' is an attribute (the name) of the 'data' variable (bad name for
a variable), not something that is independently accessible. By very
(exceptionally!) loose analogy, think of it as being something like a
field/property of an object -- you can't get to it directly (and
that's a good thing) but you can get to it by way of its object.

Internally, it is a string, just not printed as such because its
serving a different purpose than being a string datum.

Hope this helps,
Michael

On Mon, Mar 26, 2012 at 11:31 AM, Prof. Dr. Pedro Martinez Arbizu
<pmartinez at senckenberg.de> wrote:
> Me again,
> what I really dont understand is the behaivour of R here.
> notice, elem is not written in "brackets", why it is interpreted as string
> and not as a variable?
> I would expect an error "elem not found" because it is not defined as
> variable, but in contrary it is accepted as column name, see below:
>
>
>> a = c('one','two','three')
>> data =c()
>> data=cbind(elem = 2,data)
>> data
>     elem
> [1,]    2
>
>> data=cbind('elem' = 2,data)
>> data
>     elem elem
> [1,]    2    2
>
>> elem
> Fehler: Objekt 'elem' nicht gefunden
>
> pedro
>
>
>
>
>
>
>
>
>
> On 26.03.2012 17:22, Prof. Dr. Pedro Martinez Arbizu wrote:
>>
>> HI, thanks Weidong, Henrique,
>> this works for the example (may be a bad example, but it should be as
>> simple as possible),
>>
>> This is however only a workaround to name the columns of a dataframe, but
>> is not addressing the problem itself, that is, how can I substitute a string
>> (used as argument in a function) within a function call in a for-loop??
>>
>> I dont know if I have explain myself,
>>
>> something like this works well:
>>
>> > a = c('one','two','three')
>>
>>>  data= c()
>>>  for(elem in a){data=cbind('title' = elem,data)}
>>>  data
>>
>>     title   title title
>> [1,] "three" "two" "one"
>>
>>
>> So I can use 'title' = elem within the function and it will substitute
>> elem by a name, every loop by a different name of a list.
>>
>> ...but I cannot use the contrary:   elem = 'something' within the function
>> call.
>>
>> My real life problem is using randomForest() I want to balance the class
>> frequency with the argument sampsize = c( 'absent' = 23, elem = 23)
>> where elem is everytime a different name taken from a list in a for-loop.
>>
>> Regards Pedro
>>
>>
>>
>> On 26.03.2012 02:17, Weidong Gu wrote:
>>>
>>> Hi,
>>>
>>> This may help
>>>
>>>  a = c('one','two','three')
>>> data.frame(eval(substitute(rbind(var,2),list(var=a))))
>>>
>>> ?substitute
>>> ?eval
>>>
>>> Weidong Gu
>>>
>>>
>>> On Sun, Mar 25, 2012 at 5:22 PM, Pedro Martinez
>>> <Pedro.Martinez at senckenberg.de>  wrote:
>>>>
>>>> hello,
>>>> I want to iterate through a list of names and use each element as an
>>>> argument in a function. For instance:
>>>>
>>>>> a = c('one','two','three')
>>>>> data= c()
>>>>> for(elem in a){data=cbind(elem = 2,data)}
>>>>> data
>>>>
>>>>     elem elem elem
>>>> [1,]    2    2    2
>>>>
>>>> instead I want 'elem' to be substituted by the string in the list. Doing
>>>> it by hand would be:
>>>>>
>>>>> data = cbind('one'=2,data)
>>>>> data = cbind('two'=2,data)
>>>>> data = cbind('three'=2,data)
>>>>> data
>>>>
>>>>     'one' 'two' 'three'
>>>> [1,]    2    2    2
>>>>
>>>> I guess that the clue would be in sub(),gsub(), paste() or similar but I
>>>> didnt get it to work.
>>>>
>>>> I am comming from python were we woudl do something like:
>>>>>
>>>>> a = ['one','two','three']
>>>>> data = {}
>>>>> for elem in a:
>>>>>       data[elem] = 2
>>>>> data
>>>>
>>>> {'three': 2, 'two': 2, 'one': 2}
>>>>
>>>> Thanks, Pedro
>>>>
>>>>
>>>> -
>>>> Prof. Dr. P. Martinez Arbizu
>>>> DZMB-Forschungsinstitut Senckenberg
>>>>
>>>> Suedstrand 44
>>>> D-26382 Wilhelmshaven
>>>> Germany
>>>>
>>>> Tel: +49 (0)4421 9475-100
>>>> Fax: +49 (0)4421 9475-111
>>>>
>>>> Email: pmartinez at senckenberg.de
>>>>
>>>> Senckenberg Gesellschaft für Naturforschung
>>>> Rechtsfähiger Verein gemäß § 22 BGB
>>>> Senckenberganlage 25
>>>> 60325 Frankfurt
>>>> Direktorium: Prof. Dr. Dr. h.c. Volker Mosbrugger, Prof. Dr. Michael
>>>> Türkay, Dr. Johannes Heilmann, Prof. Dr. Pedro Martinez Arbizu, Prof.
>>>> Dr. Georg Zizka, Prof. Dr. Uwe Fritz
>>>> Vorsitzender des Präsidiums: Dietmar Schmid
>>>> Aufsichtsbehörde: Magistrat der Stadt Frankfurt am Main (Ordnungsamt)
>>>>
>>>> ______________________________________________
>>>> 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.
>>
>>
>>
>
>
> --
> ---------------------------------------------------------
> Prof. Dr. Pedro Martinez Arbizu
>
> Deutsches Zentrum für Marine Biodiversitätsforschung
> Senckenberg am Meer
> Südstrand 44
> 26382 Wilhelmshaven
> Tel +49 (0)4421 9475 100
> Fax +49 (0)4421 9475 111
> Email: pmartinez at senckenberg.de
> http://www.senckenberg.de/root/index.php?page_id=2141
>
> und
>
> C.v.O. Universität Oldenburg
> FK V, Institut für Biologie u. Umweltwissenschaften
> Ag Marine Biodiversität
>
> **********************************************
> Senckenberg Gesellschaft für Naturforschung
> Rechtsfähiger Verein gemäß § 22 BGB
> Senckenberganlage 25
> 60325 Frankfurt
> Direktorium: Prof. Dr. Dr. h.c. Volker Mosbrugger, Prof. Dr. Michael Türkay,
> Dr. Johannes Heilmann, Prof. Dr. Pedro Martinez Arbizu, Prof. Dr. Georg
> Zizka, Prof. Dr. Uwe Fritz
> Vorsitzender des Präsidiums: Dietmar Schmid
> Aufsichtsbehörde: Magistrat der Stadt Frankfurt am Main (Ordnungsamt)
>
> ______________________________________________
> 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