s-news
[Top] [All Lists]

Re: reversing the order of strings

To: "Khan, Sohail" <khan@cshl.edu>
Subject: Re: reversing the order of strings
From: Sundar Dorai-Raj <sundar.dorai-raj@pdf.com>
Date: Fri, 16 Jun 2006 11:38:45 -0500
Cc: s-news@lists.biostat.wustl.edu
In-reply-to: <C8696843AE995F4EA4CDC3E2B83482A901879041@mailbox02.cshl.edu>
Organization: PDF Solutions, Inc.
References: <C8696843AE995F4EA4CDC3E2B83482A901879041@mailbox02.cshl.edu>
User-agent: Mozilla Thunderbird 1.0.2 (Windows/20050317)


Khan, Sohail wrote:
Dear List,

I have character vector as follows:
"abc"
"ffd"
"sad"
How can I reverse the string so that
"cba"
"ffd"
"das"

Thanks.



Hi, Sohail,

Here's a one-liner:

> x <- c("abc", "ffd", "sad")
> sapply(lapply(x, function(x) rev(substring(x, 1:nchar(x), 1:nchar(x)))), paste, collapse = "")
[1] "cba" "dff" "das"

HTH,

--sundar

<Prev in Thread] Current Thread [Next in Thread>