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)} > dataelem 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)
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.
Henrique Dallazuanna
2012-Mar-26 00:32 UTC
[R] string substitution for argument in function
try this: `names<-`(rep(2, 3), a) On Sun, Mar 25, 2012 at 6: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.-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Seemingly Similar Threads
- [parallel] fixes load balancing of parLapplyLB
- [parallel] fixes load balancing of parLapplyLB
- [parallel] fixes load balancing of parLapplyLB
- [parallel] fixes load balancing of parLapplyLB
- [parallel-package] feature request: set default cluster type via environment variable