Re: [R] getting last 2 charcters of a string, other "text" functions?
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: > gsub(".*(..)$", "\\1", "i only want the last two characters") > > This is only a matter of finding the right regular expression. Use Google to > find a good tutorial on them. > > Carlos J. Gil Bellosta > http://www.datanalytics.com > > > Quoting t c <[EMAIL PROTECTED]>: > > > 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 > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide! > > http://www.R-project.org/posting-guide.html > > > > __ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] getting last 2 charcters of a string, other "text" functions?
"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, nchar(x)) [1] "bcd" "xyz" efg __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] getting last 2 charcters of a string, other "text" functions?
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, Tobias > > >- > > [[alternative HTML version deleted]] > >__ >R-help@stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > > > > __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] getting last 2 charcters of a string, other "text" functions?
gsub(".*(..)$", "\\1", "i only want the last two characters") This is only a matter of finding the right regular expression. Use Google to find a good tutorial on them. Carlos J. Gil Bellosta http://www.datanalytics.com Quoting t c <[EMAIL PROTECTED]>: > 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 > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] getting last 2 charcters of a string, other "text" functions?
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 list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] getting last 2 charcters of a string, other "text" functions?
?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 character string? What is > the appropriate function? > > > > - > > [[alternative HTML version deleted]] > > __ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > -- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 452-1424 (M, W, F) fax: (917) 438-0894 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] getting last 2 charcters of a string, other "text" functions?
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 https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html