[R] string substitution for argument in function

Pedro Martinez Pedro.Martinez at senckenberg.de
Sun Mar 25 23:22:07 CEST 2012


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)



More information about the R-help mailing list