[R] how to replace the following string

jim holtman jholtman at gmail.com
Mon Jun 27 19:22:43 CEST 2011


Is this what you want:

> test
[1] "H.1.Y" "N.0.E" "G.1.N" "E.0.P" "W.2.G" "W.4.G" "W.3.W" "W.0.N" "D.1.H"
> sub("\\.([0-9]+)\\.", "{\\1}", test)
[1] "H{1}Y" "N{0}E" "G{1}N" "E{0}P" "W{2}G" "W{4}G" "W{3}W" "W{0}N" "D{1}H"
>


On Mon, Jun 27, 2011 at 1:18 PM, Changbin Du <changbind at gmail.com> wrote:
> HI,  Dear R-community,
>
> I have one vector of the following string,
>
>> test<-c("H.1.Y", "N.0.E", "G.1.N", "E.0.P", "W.2.G", "W.4.G", "W.3.W",
> "W.0.N", "D.1.H")
>> test
> [1] "H.1.Y" "N.0.E" "G.1.N" "E.0.P" "W.2.G" "W.4.G" "W.3.W" "W.0.N" "D.1.H"
>
> *I want to change them into*
>
>  "H{1}Y" "N{0}E" "G{1}N" "W{2}G" "E{0}P" "W{4}G" "W{3}W" "W{0}N" "D{1}H"
>
>
> I tried the following codes:
>
>> new_t<-gsub(".?.", "{?}", test)
>> new_t
> [1] "{?}{?}{?}" "{?}{?}{?}" "{?}{?}{?}" "{?}{?}{?}" "{?}{?}{?}" "{?}{?}{?}"
> [7] "{?}{?}{?}" "{?}{?}{?}" "{?}{?}{?}"
>
>> new_t<-gsub(".N.", "{N}", test)
>> new_t
> [1] "H.1.Y" "N.0.E" "G.1.N" "E.0.P" "W.2.G" "W.4.G" "W.3.W" "W.0.N" "D.1.H"
>
>> new_t<-gsub(".*.", "{*}", test)
>> new_t
> [1] "{*}" "{*}" "{*}" "{*}" "{*}" "{*}" "{*}" "{*}" "{*}"
>
>
> Can anyone help me with this?
>
> Thanks!
>
> --
> Sincerely,
> Changbin
> --
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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?



More information about the R-help mailing list