Skip to content

Coercing character string to function argument ?

3 messages · Derek Eder, Brian Ripley, Adaikalavan Ramasamy

#
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
parse is your friend:

i <- "1:5, 20:25, 50"
[1]  1  2  3  4  5 20 21 22 23 24 25 50
On Wed, 28 Aug 2002, Derek Eder wrote:

            

  
    
#
[1] TRUE


----- Original Message -----
From: "Derek Eder" <Derek.Eder at neuro.gu.se>
To: "<" <r-help at stat.math.ethz.ch>
Sent: Wednesday, August 28, 2002 12:32 PM
Subject: [R] Coercing character string to function argument ?
subsetting a vector.
down!
-.-.-
http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
_._
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._