Quantcast

manipulating a character "with.several.periods..01"

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

manipulating a character "with.several.periods..01"

chuck.01
This post has NOT been accepted by the mailing list yet.
Hi,

how do I make this character:

"Lab.01.Entrance..1999102."

look like this

"Lab 01 Entrance (1999102)"

??

I thought about strsplit() and then a bunch of paste statements, but there has to be an easier way.
Thanks for any ideas.
-Chuck
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: manipulating a character "with.several.periods..01"

arun kirshna
This post has NOT been accepted by the mailing list yet.
HI,
Try this:
new1<-"Lab.01.Entrance..1999102."
new2<-gsub("\\."," ",new1)
 gsub("(.*\\s.*\\s.*)\\s+(.*)\\s","\\1(\\2)",new2)
#[1] "Lab 01 Entrance (1999102)"
#or
gsub("(.*Entrance{0,1})\\s+(.*)\\s","\\1 (\\2)",new2)
#[1] "Lab 01 Entrance (1999102)"
A.K.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: manipulating a character "with.several.periods..01"

arun kirshna
This post has NOT been accepted by the mailing list yet.
In reply to this post by chuck.01
Hi,

Another one step approach:
new1<-"Lab.01.Entrance..1999102."
gsub("(.*)\\.(.*)\\.(.*)\\.\\.(.*)\\.","\\1 \\2 \\3 (\\4)",new1)
#[1] "Lab 01 Entrance (1999102)"
A.K.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: manipulating a character "with.several.periods..01"

chuck.01
This post has NOT been accepted by the mailing list yet.
Thank you greatly Arun.

Could you please tell me what this is generally called:

\.(.*)\\.(.*)\\.\\.

it makes no sense, and I would like to find some documentation so I can understand it.
Thanks so much!

arun kirshna wrote
Hi,

Another one step approach:
new1<-"Lab.01.Entrance..1999102."
gsub("(.*)\\.(.*)\\.(.*)\\.\\.(.*)\\.","\\1 \\2 \\3 (\\4)",new1)
#[1] "Lab 01 Entrance (1999102)"
A.K.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: manipulating a character "with.several.periods..01"

chuck.01
This post has NOT been accepted by the mailing list yet.
are these "regular expressions" used in the grep() and gsub() functions?


chuck.01 wrote
Thank you greatly Arun.

Could you please tell me what this is generally called:

\.(.*)\\.(.*)\\.\\.

it makes no sense, and I would like to find some documentation so I can understand it.
Thanks so much!

arun kirshna wrote
Hi,

Another one step approach:
new1<-"Lab.01.Entrance..1999102."
gsub("(.*)\\.(.*)\\.(.*)\\.\\.(.*)\\.","\\1 \\2 \\3 (\\4)",new1)
#[1] "Lab 01 Entrance (1999102)"
A.K.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: manipulating a character "with.several.periods..01"

arun kirshna
This post has NOT been accepted by the mailing list yet.
HI,

Yes, these are regular expressions.

#To understand it, may be it is easier to split the whole gsub()
#Here, the expression inside the first quotes look for the pattern to match; the expression inside the second quote is #the replacement.
#For example, if I want to return only "Lab" or "01" or "Entrance"
gsub("(.*)\\.(.*)\\.(.*)\\.\\.(.*)\\.","\\1",new1)
#[1] "Lab"
 gsub("(.*)\\.(.*)\\.(.*)\\.\\.(.*)\\.","\\2",new1)
#[1] "01"
 gsub("(.*)\\.(.*)\\.(.*)\\.\\.(.*)\\.","\\3",new1)
#[1] "Entrance"
This can be also achieved with a simple expression
 gsub("(Lab{0,1}).*","\\1",new1)
#[1] "Lab"
#In the whole expression
gsub("(.*)\\.(.*)\\.(.*)\\.\\.(.*)\\.","\\1 \\2 \\3 (\\4)",new1)
new1<-"Lab.01.Entrance..1999102."
"\\1" (first parenthesis from left) will return "Lab", escaped "." with "\\."
".*" means zero or more of any character.
If you follow the logic, dots are replaced with spaces inside the second " ".

Hope it helps.
A.K.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: manipulating a character "with.several.periods..01"

arun kirshna
This post has NOT been accepted by the mailing list yet.
In reply to this post by chuck.01
Hi,

If I change the string a little bit, and if I want to get the same result or slightly different results:
new2<- "Lab.01_Entrance._1946."
 gsub("(.*)\\.(.*)\\_(.*)\\.\\_(.*)\\.","\\1 \\2 \\3 (\\4)",new2)
#[1] "Lab 01 Entrance (1946)"
gsub("(.*)\\.(.*)\\_(.*)\\.\\_(.*)\\.","\\1 (\\2) \\3 (\\4)",new2)
#[1] "Lab (01) Entrance (1946)"
A.K.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: manipulating a character "with.several.periods..01"

chuck.01
This post has NOT been accepted by the mailing list yet.
You are awesome; Thanks for this great starting point.  I will take it from here.  
Cheers!

arun kirshna wrote
Hi,

If I change the string a little bit, and if I want to get the same result or slightly different results:
new2<- "Lab.01_Entrance._1946."
 gsub("(.*)\\.(.*)\\_(.*)\\.\\_(.*)\\.","\\1 \\2 \\3 (\\4)",new2)
#[1] "Lab 01 Entrance (1946)"
gsub("(.*)\\.(.*)\\_(.*)\\.\\_(.*)\\.","\\1 (\\2) \\3 (\\4)",new2)
#[1] "Lab (01) Entrance (1946)"
A.K.
Loading...