Re: [R] find and replace string

2011-12-02 Thread Sarah Goslee
You've been given a workable solution already, but here's a one-liner:

> x <- c('sta_+1+0_field2ndtry_$01.cfg' , 
> 'sta_+B+0_field2ndtry_$01.cfg' , 'sta_+1+0_field2ndtry_$01.cfg' , 
> 'sta_+9+0_field2ndtry_$01.cfg')
> sapply(1:length(x), function(i)gsub("\\+(.*)\\+.", paste("\\+\\1\\+", i, 
> sep=""), x[i]))
[1] "sta_+1+1_field2ndtry_$01.cfg" "sta_+B+2_field2ndtry_$01.cfg"
[3] "sta_+1+3_field2ndtry_$01.cfg" "sta_+9+4_field2ndtry_$01.cfg"


Sarah, fan of regular expressions

On Fri, Dec 2, 2011 at 6:30 AM, Alaios  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_$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_$01.cfg
>
> sta_+1+1_field2ndtry_$01.cfg
>
> sta_+1+2_field2ndtry_$01.cfg
>
> sta_+1+3_field2ndtry_$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

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] find and replace string

2011-12-02 Thread Alaios
You are too good :)
Thanks a lot have a nice weekend

B.R
Alex




 From: jim holtman 

Cc: "R-help@r-project.org"  
Sent: Friday, December 2, 2011 1:51 PM
Subject: Re: [R] find and replace string

try this:

> x <- c('sta_+1+0_field2ndtry_$01.cfg'
+      , 'sta_+1+0_field2ndtry_$01.cfg'
+      , 'sta_+1-0_field2ndtry_$01.cfg'
+      , 'sta_+1+0_field2ndtry_$01.cfg'
+      )
> # find matching fields
> values <- grep("[^+]*\\+[^+]*\\+0", x, value = TRUE)
> # split into two pieces
> splitValues <- sub("([^+]*\\+[^+]*\\+)0(.*)", "\\1^\\2", values)
> for (i in splitValues){
+     for (j in 0:3){
+         print(sub("\\^", j, i))
+     }
+ }
[1] "sta_+1+0_field2ndtry_$01.cfg"
[1] "sta_+1+1_field2ndtry_$01.cfg"
[1] "sta_+1+2_field2ndtry_$01.cfg"
[1] "sta_+1+3_field2ndtry_$01.cfg"
[1] "sta_+1+0_field2ndtry_$01.cfg"
[1] "sta_+1+1_field2ndtry_$01.cfg"
[1] "sta_+1+2_field2ndtry_$01.cfg"
[1] "sta_+1+3_field2ndtry_$01.cfg"
[1] "sta_+1+0_field2ndtry_$01.cfg"
[1] "sta_+1+1_field2ndtry_$01.cfg"
[1] "sta_+1+2_field2ndtry_$01.cfg"
[1] "sta_+1+3_field2ndtry_$01.cfg"


> 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_$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_$01.cfg
>
> sta_+1+1_field2ndtry_$01.cfg
>
> sta_+1+2_field2ndtry_$01.cfg
>
> sta_+1+3_field2ndtry_$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
>
>        [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] find and replace string

2011-12-02 Thread christiaan pauw
If the length of the fists part is constant (the "sta_+1+" part) the
you can use substr()




On 2 December 2011 13:30, Alaios  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_$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_$01.cfg
>
> sta_+1+1_field2ndtry_$01.cfg
>
> sta_+1+2_field2ndtry_$01.cfg
>
> sta_+1+3_field2ndtry_$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
>



--
Christiaan Pauw
Nova Institute
www.nova.org.za

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] find and replace string

2011-12-02 Thread jim holtman
try this:

> x <- c('sta_+1+0_field2ndtry_$01.cfg'
+  , 'sta_+1+0_field2ndtry_$01.cfg'
+  , 'sta_+1-0_field2ndtry_$01.cfg'
+  , 'sta_+1+0_field2ndtry_$01.cfg'
+  )
> # find matching fields
> values <- grep("[^+]*\\+[^+]*\\+0", x, value = TRUE)
> # split into two pieces
> splitValues <- sub("([^+]*\\+[^+]*\\+)0(.*)", "\\1^\\2", values)
> for (i in splitValues){
+ for (j in 0:3){
+ print(sub("\\^", j, i))
+ }
+ }
[1] "sta_+1+0_field2ndtry_$01.cfg"
[1] "sta_+1+1_field2ndtry_$01.cfg"
[1] "sta_+1+2_field2ndtry_$01.cfg"
[1] "sta_+1+3_field2ndtry_$01.cfg"
[1] "sta_+1+0_field2ndtry_$01.cfg"
[1] "sta_+1+1_field2ndtry_$01.cfg"
[1] "sta_+1+2_field2ndtry_$01.cfg"
[1] "sta_+1+3_field2ndtry_$01.cfg"
[1] "sta_+1+0_field2ndtry_$01.cfg"
[1] "sta_+1+1_field2ndtry_$01.cfg"
[1] "sta_+1+2_field2ndtry_$01.cfg"
[1] "sta_+1+3_field2ndtry_$01.cfg"

On Fri, Dec 2, 2011 at 6:30 AM, Alaios  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_$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_$01.cfg
>
> sta_+1+1_field2ndtry_$01.cfg
>
> sta_+1+2_field2ndtry_$01.cfg
>
> sta_+1+3_field2ndtry_$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
>
>        [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.