I wish to obtain the right-most n characters of a character string? What is
the appropriate function?
-
[[alternative HTML version deleted]]
__
R-help@stat.math.ethz.ch mailing list
?nchar
?substr
rightmost - function(x, y){substr(x, start=nchar(x) - (y - 1),
stop=nchar(x))}
x - c(asfef, qwerty, yuiop[, b, stuff.blah.yech)
rightmost(x, 2)
[1] ef ty p[ b ch
rightmost(x, 3)
[1] fef rty op[ b ech
t c wrote:
I wish to obtain the right-most n characters of a
t c wrote:
I wish to obtain the right-most n characters of a character string? What is
the appropriate function?
See ?nchar ?substr
k - 2
x - abcdef
nc - nchar(x)
substr(x, nc - k + 1, nc)
HTH,
--sundar
__
R-help@stat.math.ethz.ch mailing
t c wrote:
I wish to obtain the right-most n characters of a character string? What is
the appropriate function?
You could make one yourself:
rightmostn - function(x, n){
res - substr(x, nchar(x)-n+1, nchar(x))
return(res)
}
magic - hocuspocus
rightmostn(magic, 5)
[1] pocus
HTH,
t c [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
I wish to obtain the right-most n characters of a character string? What
is the appropriate function?
substr will work:
x - c(abcd, xyz)
N - 2
substr(x, nchar(x)-N+1, nchar(x))
[1] cd yz
N - 3
substr(x, nchar(x)-N+1,
Note that this one can be slightly simplified by using sub instead of gsub
(since you only will have one match anyways) and the $ is not needed
since .* will consume the maximal matching string:
sub(.*(..), \\1, mystring)
On 10/31/05, Carlos J. Gil Bellosta [EMAIL PROTECTED] wrote: