Message-ID: <9CA3F558-8454-4CED-9604-5FC327C7E046@comcast.net>
Date: 2011-10-16T16:42:29Z
From: David Winsemius
Subject: Which function to use: grep, replace, substr etc.?
In-Reply-To: <1318782958087-3909871.post@n4.nabble.com>
On Oct 16, 2011, at 12:35 PM, syrvn wrote:
> Hello,
>
> I have a simple question but I don't know which method is best to
> use for my
> problem.
>
> I have the following strings:
>
> str1 <- "My_name_is_peter"
> str2 <- "what_is_your_surname_peter"
>
> I would like to apply predefined abbreviations for peter=p and
> name=n to
> both strings
> so that the new strings look like the followings:
>
> str1: "My_n_is_p"
> str2: "what_is_your_surn_p"
>
> Which method is the best to use for that particular problem?
?sub # on same page as grep
> sub("(p)eter", "\\1", vec)
[1] "My_name_is_p" "what_is_your_surname_p"
--
David Winsemius, MD
West Hartford, CT