[R] Which function to use: grep, replace, substr etc.?

David Winsemius dwinsemius at comcast.net
Sun Oct 16 18:42:29 CEST 2011


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



More information about the R-help mailing list