[R] Coercing character string to function argument ?

ripley@stats.ox.ac.uk ripley at stats.ox.ac.uk
Wed Aug 28 13:43:58 CEST 2002


parse is your friend:

i <- "1:5, 20:25, 50"
> eval(parse(text=paste("c(",i, ")")))
 [1]  1  2  3  4  5 20 21 22 23 24 25 50


On Wed, 28 Aug 2002, Derek Eder wrote:

> I need to turn the character string:  "1:5, 20:25, 50"  into indices for subsetting a vector.
>
> For example:
>
> indices <- "1:5, 20:25, 50, 99"
> vector <- rnorm(100,0,1)
>
> # Please R, show me the way to achieve this:  vector[indices]
>
>
> I thank you angels in advance for rushing in where this fool got bogged down!
>
> - Derek
>
> Derek N. Eder
> Göteborgs Universitet
> Institutionen för klinisk neurovetenskap
> Klinisk Neurofysiologi
> Sahlgrenska universitetssjukhuset SS/SU
> Blå straket 7, vån 3
> SE 413 45  Göteborg
> Sverige
> Tlf. +46 (031) 34  2-6139  (office)
> Tlf. +46 (031) 34 2-1283  (laboratory)
> Tlf. +46 0709 / 7 2-1283 (mobil)
> Fax. +46 (031) 82 81 63
> derek.eder at neuro.gu.se
>
>
> Gothenburg University
> Institute of Clinical Neuroscience,
> Department of Clinical Neurophysiology
> Salhgrenska Hospital  SU/SS
> SE 413 45  Göteborg
> Sweden
>
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list