t c
2005-Oct-31 15:37 UTC
[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]]
Chuck Cleland
2005-Oct-31 15:48 UTC
[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 at 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
Sundar Dorai-Raj
2005-Oct-31 15:49 UTC
[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
Carlos J. Gil Bellosta
2005-Oct-31 15:52 UTC
[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 <quantpm at yahoo.com>:> I wish to obtain the right-most n characters of a character string? > What is the appropriate function? > > > > --------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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 >
Earl F. Glynn
2005-Oct-31 15:59 UTC
[R] getting last 2 charcters of a string, other "text" functions?
"t c" <quantpm at yahoo.com> wrote in message news:20051031153755.80990.qmail at web35006.mail.mud.yahoo.com...> I wish to obtain the right-most n characters of a character string? Whatis 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
Tobias Verbeke
2005-Oct-31 16:01 UTC
[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 at 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 > > > >
Gabor Grothendieck
2005-Oct-31 16:24 UTC
[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 <cgb at datanalytics.com> 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 <quantpm at yahoo.com>: > > > I wish to obtain the right-most n characters of a character string? > > What is the appropriate function? > > > > > > > > --------------------------------- > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at 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 at 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 >