[R] string substitution for argument in function

Prof. Dr. Pedro Martinez Arbizu pmartinez at senckenberg.de
Mon Mar 26 17:22:20 CEST 2012


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)



More information about the R-help mailing list