[R] substring/strsplit question

davidr at rhotrading.com davidr at rhotrading.com
Wed Oct 29 22:10:00 CET 2008


How about

> x <- c("2E","5W","12H")
> substr(x, nchar(x), nchar(x))
[1] "E" "W" "H"
> 
> substr(x, 1, nchar(x)-1)
[1] "2"  "5"  "12"

-- David


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Erin Hodgess
Sent: Wednesday, October 29, 2008 3:58 PM
To: r-help at stat.math.ethz.ch
Subject: [R] substring/strsplit question

Dear R People:

Here is a toy example:

> x <- c("2E","5W","12H")
> substr(x,2,2)
[1] "E" "W" "2"
>

Sometimes x has 3 elements, sometimes 2.  I want to extract the last
element, and then extract the other 1 or 2 elements.

How can I do this, please?

TIA,
Sincerely,
Erin


-- 
Erin Hodgess
Associate Professor
Department of Computer and Mathematical Sciences
University of Houston - Downtown
mailto: erinm.hodgess at gmail.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.



More information about the R-help mailing list