[R] join chars in loops

Bert Gunter gunter.berton at gene.com
Fri Apr 25 17:23:43 CEST 2008


...
That is, use the construction

thelist <- lapply(1:11,function(i){...return something}) 

I would go further then Dmitris and say that this should be the canonical
answer to this frequently asked question and that perhaps in 7.21 in the R
FAQ (Version 2.6.2 -- I haven't yet upgraded) -- "How can I turn a string
into a variable?" --  this could be explicitly stated (it's in there, but
not quite so explicitly). Or perhaps it might be a separate FAQ
question:"How to create new variables from their character string names in a
loop?" This might make it a little easier for people to find and a little
less likely that the question will get asked on the list -- though I must
agree with those who have lamented that it often seems pointless to make
such efforts because people don't bother to use the resources anyway. Ah,
but that's just the grumbling of an old reprobate...

Cheers,
Bert

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Dimitris Rizopoulos
Sent: Friday, April 25, 2008 8:07 AM
To: Dirkheld
Cc: r-help at r-project.org
Subject: Re: [R] join chars in loops

look at ?assign(), e.g.,

for (i in 1:11) {
    assign(paste("var", i, sep = ""), i)
}
var1
var2
var10

but I'd prefer to use a list. I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "Dirkheld" <dirk.bollen at soc.kuleuven.be>
To: <r-help at r-project.org>
Sent: Friday, April 25, 2008 4:40 PM
Subject: [R] join chars in loops


>
> Hi,
>
> Is it possible to create new vars in a loop
>
> For instance:
>
> for (i in 1:11)
> var'i' <- assign something
>
> output =
> var1
> var2
> ...
> var11
>
> Is there a way to combine/join the value of 'i' to the string/char 
> var in de
> loop.
>
> -- 
> View this message in context: 
> http://www.nabble.com/join-chars-in-loops-tp16895854p16895854.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
> 


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

______________________________________________
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