[R] Concatenate a mix of numbers and letters to create a vector name

Greg Snow Greg.Snow at imail.org
Tue Aug 10 17:55:11 CEST 2010


Others gave you some examples using assign, which is the same information as in the FAQ.  But I expect that you will be better served by using a list (you can use the paste function to create the names for the list elements) rather than assign and global variables.

Something like:

mylist <- list()

for (i in c(1950, 1960, 1970) ) {
  for (j in 1:12) {
    mylist[[ paste( 'tmax.', i, j, sep='' ) ]] <- somefunction(i, j)
  }
}

Then you can access a single result like:

mylist[['tmax.15012']]

or you can do something with all the output using something like:

sapply( mylist, summary )

hope this helps,

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: Panos Hadjinicolaou [mailto:p.hadjinicolaou at cyi.ac.cy]
> Sent: Tuesday, August 10, 2010 6:07 AM
> To: Greg Snow; r-help at r-project.org
> Subject: RE: [R] Concatenate a mix of numbers and letters to create a
> vector name
> 
> I was not aware of the R-FAQ, it seems to have some very useful tips,
> thanks for pointing there.
> 
> Regarding the 7.21 in the FAQ, I read it a few times but it did not
> lead me anywhere. For the moment I am blaming my inexperience with some
> R basics, I will come back after I do some more serious studying.
> 
> Thanks again,
> 
> Panos
> 
> --------------------------------------------------------
> Dr Panos Hadjinicolaou
> 
> Energy Environment & Water Research Center (EEWRC)
> The Cyprus Institute
> 
> ---------------------------------------------------------
> 
> 
>   _____
> From: Greg Snow [mailto:Greg.Snow at imail.org]
> To: Panos Hadjinicolaou [mailto:p.hadjinicolaou at cyi.ac.cy], r-help at r-
> project.org [mailto:r-help at r-project.org]
> Sent: Wed, 28 Jul 2010 00:39:41 +0300
> Subject: RE: [R] Concatenate a mix of numbers and letters to create a
> vector name
> 
> This is a frequently asked/answered question (7.21 in the FAQ).  What
> searching did you do and why did it not find this FAQ or previous
> discussion of it?  How could the documentation/search/etc. be improved
> so that you (and the next n people with this question) will find the
> answer easier?
> 
> The most important of part of the FAQ answer is the last section where
> it points out that using a list will be much simpler.  You mentioned in
> the first post that you were doing this in a loop, just start with an
> empty list, use paste (or sprintf) to create the name, and then assign
> it as a new element of the list with that name (e.g. mylist[[
> sprintf('tmax.%d%d', var1, var2) ]] <- outputfromcomputations ).
> 
> --
> Gregory (Greg) L. Snow Ph.D.
> Statistical Data Center
> Intermountain Healthcare
> greg.snow at imail.org
> 801.408.8111
> 
> > -----Original Message-----
> > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> > project.org] On Behalf Of Panos Hadjinicolaou
> > Sent: Monday, July 26, 2010 8:24 AM
> > To: r-help at r-project.org
> > Subject: Re: [R] Concatenate a mix of numbers and letters to create a
> > vector name
> >
> > Thanks for the reply. Indeed the paste function results in
> > concatenation:
> >  > paste(c("tmax.", 1950, 12), collapse="")
> > [1] "tmax.195012"
> >
> > but  I am looking for a way to subsequently get rid of the - " - in
> > order to  use tmax.195012 as an object (e.g. to define a vector with
> > that name).  Any ideas?
> >
> > Thanks,
> >
> > Panos
> >
> >   _____
> >
> > From: Dimitris Rizopoulos [mailto:d.rizopoulos at erasmusmc.nl]
> > To: Panos Hadjinicolaou [mailto:p.hadjinicolaou at cyi.ac.cy]
> > Cc: r-help at r-project.org
> > Sent: Mon, 26 Jul 2010 16:48:31 +0300
> > Subject: Re: [R] Concatenate a mix of numbers and letters to create a
> > vector name
> >
> > have a look at function paste(), i.e., ?paste
> >
> >
> >   I hope it helps.
> >
> >   Best,
> >   Dimitris
> >
> >
> >   On 7/26/2010 3:44 PM, Panos Hadjinicolaou wrote:
> >   > Dear all,
> >   >
> >   > I am trying to create a vector name, for example tmax.195012 from
> > tmax., 1950 and 12. Obviously I don't wish to simply type it because
> > the 3 name components are changing in each iteration within a loop.
> Is
> > there any way of concatenating those 3 components (which are a
> mixture
> > of numbers and letters)?
> >   >
> >   > Thanks for reading,
> >   >
> >   > Panos
> >   >
> >   > -----------------------------------------------------------------
> >   > Dr Panos Hadjinicolaou
> >   >
> >   > Energy Environment&  Water Research Center (EEWRC)
> >   > The Cyprus Institute
> >   > -----------------------------------------------------------------
> -
> >   >  [[alternative HTML version deleted]]
> >   >
> >   > ______________________________________________
> >   > 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.
> >   >
> >
> >   --
> >   Dimitris Rizopoulos
> >   Assistant Professor
> >   Department of Biostatistics
> >   Erasmus University Medical Center
> >
> >   Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
> >   Tel: +31/(0)10/7043478
> >   Fax: +31/(0)10/7043014
> > 	[[alternative HTML version deleted]]
> >
> > ______________________________________________
> > 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