[R] string substitution for argument in function
Weidong Gu
anopheles123 at gmail.com
Mon Mar 26 02:17:42 CEST 2012
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.
More information about the R-help
mailing list