[R] substring/strsplit question

2008-10-29 Thread Erin Hodgess
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: [EMAIL PROTECTED]

__
R-help@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.


Re: [R] substring/strsplit question

2008-10-29 Thread Erik Iverson

## untested

last - tail(x, n = 1)
first - head(x, n = length(x) - 1)

Erin Hodgess wrote:

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




__
R-help@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.


Re: [R] substring/strsplit question

2008-10-29 Thread Henrique Dallazuanna
Try this:

library(gsubfn)

## The last character
strapply(x, (.)$, simplify = TRUE)

## The last two character
strapply(x, (..)$, simplify = TRUE)


On Wed, Oct 29, 2008 at 6:57 PM, Erin Hodgess [EMAIL PROTECTED]wrote:

 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: [EMAIL PROTECTED]

 __
 R-help@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.




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] substring/strsplit question

2008-10-29 Thread davidr
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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
On Behalf Of Erin Hodgess
Sent: Wednesday, October 29, 2008 3:58 PM
To: [EMAIL PROTECTED]
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: [EMAIL PROTECTED]

__
R-help@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.

__
R-help@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.


Re: [R] substring/strsplit question

2008-10-29 Thread Marc Schwartz
on 10/29/2008 03:57 PM Erin Hodgess wrote:
 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


Hi Erin,

Is this what you want?


# Get the last character
 gsub(.*(.)$, \\1, x)
[1] E W H


# Get the others
 gsub((^.*).$, \\1, x)
[1] 2  5  12


See ?gsub

HTH,

Marc Schwartz

__
R-help@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.


Re: [R] substring/strsplit question

2008-10-29 Thread Gabor Grothendieck
Assuming that by elements you mean characters (2E is the first
element of x but E is the last character in x[1]) then this will
create a character matrix of dimensions: length(x) by 2
such that each row corresponds to one component of x
and the second column in that row holds its last character
while the first column in that row holds a string of the prior
characters.

 x - c(2E,5W,12H)
 library(gsubfn)

 strapply(x, (.+)(.)$, c, simplify = rbind)
 [,1] [,2]
[1,] 2  E
[2,] 5  W
[3,] 12 H

The above assumes the latest version of gsubfn
on CRAN.


On Wed, Oct 29, 2008 at 4:57 PM, Erin Hodgess [EMAIL PROTECTED] wrote:
 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: [EMAIL PROTECTED]

 __
 R-help@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.


__
R-help@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.