[R] find and replace string
Sarah Goslee
sarah.goslee at gmail.com
Fri Dec 2 15:25:57 CET 2011
You've been given a workable solution already, but here's a one-liner:
> x <- c('sta_+1+0_field2ndtry_0000$01.cfg' , 'sta_+B+0_field2ndtry_0000$01.cfg' , 'sta_+1+0_field2ndtry_0000$01.cfg' , 'sta_+9+0_field2ndtry_0000$01.cfg')
> sapply(1:length(x), function(i)gsub("\\+(.*)\\+.", paste("\\+\\1\\+", i, sep=""), x[i]))
[1] "sta_+1+1_field2ndtry_0000$01.cfg" "sta_+B+2_field2ndtry_0000$01.cfg"
[3] "sta_+1+3_field2ndtry_0000$01.cfg" "sta_+9+4_field2ndtry_0000$01.cfg"
Sarah, fan of regular expressions
On Fri, Dec 2, 2011 at 6:30 AM, Alaios <alaios at yahoo.com> wrote:
> Dear all,
> I would like to search in a string for the second occurrence of a symbol and replace the symbol after it
>
> For example my strings look like
>
> sta_+1+0_field2ndtry_0000$01.cfg
>
> I want to find the digit that comes after the second +, in that case is zero
> and then over a loop create the strings below
>
> sta_+1+0_field2ndtry_0000$01.cfg
>
> sta_+1+1_field2ndtry_0000$01.cfg
>
> sta_+1+2_field2ndtry_0000$01.cfg
>
> sta_+1+3_field2ndtry_0000$01.cfg
>
> and so on..
> I have already tried strsplit but this will make things more complex...
>
> Could you please help me with that?
>
> B.R
> Alex
>
--
Sarah Goslee
http://www.functionaldiversity.org
More information about the R-help
mailing list