Dear all, Are there any functions in R to reverse the order of the string. smth like reverse("abc") to get "cba"? Thanks a lot. [[alternative HTML version deleted]]
Hi, I've no idea if there is a function for doing it, but if not, the following seems to work a = "this is a string" paste(rev(substring(a,1:nchar(a),1:nchar(a))),collapse="") Martyn -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Trafim Vanishek Sent: 14 July 2010 11:13 To: r-help at r-project.org Subject: [R] reverse string Dear all, Are there any functions in R to reverse the order of the string. smth like reverse("abc") to get "cba"? Thanks a lot. [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. ________________________________________________________________________ This e-mail has been scanned for all viruses by Star.\ _...{{dropped:12}}
On Wed, 2010-07-14 at 12:13 +0200, Trafim Vanishek wrote:> Dear all, > > Are there any functions in R to reverse the order of the string. > smth like reverse("abc") to get "cba"?> paste(rev(strsplit("abc", split = "")[[1]]), collapse = "")[1] "cba" If your real data is a vector of strings to reverse then strings <- c("abc","abc","abc") sapply(strsplit(strings, split = ""), function(str) {paste(rev(str), collapse = "")}) is one way to do this. HTH G -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] ucl.ac.uk/~ucfagls UK. WC1E 6BT. [w] freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Hi Quick glance to strsplit help page strReverse <- function(x) sapply(lapply(strsplit(x, NULL), rev), paste, collapse="") Regards Petr r-help-bounces at r-project.org napsal dne 14.07.2010 12:36:12:> On Wed, 2010-07-14 at 12:13 +0200, Trafim Vanishek wrote: > > Dear all, > > > > Are there any functions in R to reverse the order of the string. > > smth like reverse("abc") to get "cba"? > > > paste(rev(strsplit("abc", split = "")[[1]]), collapse = "") > [1] "cba" > > If your real data is a vector of strings to reverse then > > strings <- c("abc","abc","abc") > sapply(strsplit(strings, split = ""), > function(str) {paste(rev(str), collapse = "")}) > > is one way to do this. > > HTH > > G > > -- > %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% > Dr. Gavin Simpson [t] +44 (0)20 7679 0522 > ECRC, UCL Geography, [f] +44 (0)20 7679 0565 > Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk > Gower Street, London [w] ucl.ac.uk/~ucfagls > UK. WC1E 6BT. [w] freshwaters.org.uk > %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guideR-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
On Wed, Jul 14, 2010 at 6:13 AM, Trafim Vanishek <rdapamoga at gmail.com> wrote:> Dear all, > > Are there any functions in R to reverse the order of the string. > smth like reverse("abc") to get "cba"? >Suppose data.frame DF has a column for which we wish to reverse each component. Then using the builtin month.abb try this:> DF <- data.frame(month = month.abb) > library(sqldf) > sqldf("select reverse(month) from DF")reverse(month) 1 naJ 2 beF 3 raM 4 rpA 5 yaM 6 nuJ 7 luJ 8 guA 9 peS 10 tcO 11 voN 12 ceD