HI Christofer, Don't know what is wrong in your side. I copy and pasted the same code and got the same answer as I emailed you.
CH <- c("MRTZIt", "MRTZIT", "PPBZJG", "ppbzJG") CH1<-sort(CH,decreasing=TRUE) CH1 #[1] "ppbzJG" "PPBZJG" "MRTZIt" "MRTZIT" chnew<-CH1 chnew[duplicated(toupper(CH1))]<- CH1[duplicated(toupper(CH1),fromLast=TRUE)] chnew #[1] "ppbzJG" "ppbzJG" "MRTZIt" "MRTZIt" sort(chnew) #[1] "MRTZIt" "MRTZIt" "ppbzJG" "ppbzJG" A.K. ________________________________ From: Christofer Bogaso <bogaso.christo...@gmail.com> To: arun <smartpink...@yahoo.com> Cc: R help <r-help@r-project.org>; Bert Gunter <gunter.ber...@gene.com> Sent: Sunday, August 11, 2013 2:19 PM Subject: Re: [R] Working with string Hello Arun, Thank you for your prompt reply. However if I use your new code, I am not getting the answer which you provided: > CH <- c("MRTZIt", "MRTZIT", "PPBZJG", "ppbzJG") > CH [1] "MRTZIt" "MRTZIT" "PPBZJG" "ppbzJG" > CH1<-sort(CH,decreasing=TRUE) > chnew<-CH1 > chnew[duplicated(toupper(CH1))]<- CH1[duplicated(toupper(CH1),fromLast=TRUE)] > sort(chnew) [1] "MRTZIT" "MRTZIT" "PPBZJG" "PPBZJG" Probably that I am missing something. Can you please help me out? Thanks and regards, On Sun, Aug 11, 2013 at 11:51 PM, arun <smartpink...@yahoo.com> wrote: > >Hi Christofer, > >I didn't test this extensively. Looks like this works for the example you >showed. > > > CH1<-sort(CH,decreasing=TRUE) >chnew<-CH1 >chnew[duplicated(toupper(CH1))]<- CH1[duplicated(toupper(CH1),fromLast=TRUE)] >sort(chnew) >#[1] "MRTZIt" "MRTZIt" "ppbzJG" "ppbzJG" >A.K. > > > > >________________________________ >From: Christofer Bogaso <bogaso.christo...@gmail.com> >To: Bert Gunter <gunter.ber...@gene.com> >Cc: arun <smartpink...@yahoo.com>; R help <r-help@r-project.org> >Sent: Sunday, August 11, 2013 1:54 PM >Subject: Re: [R] Working with string > > > > >Thanks Bert and Arun for your help. > >As Bert already pointed out, Arun's code is working well, however except this: > >> CH <- c("MRTZIt", "MRTZIT", "PPBZJG", "ppbzJG") >> chnew<-CH >> chnew[duplicated(toupper(CH))]<-CH[duplicated(toupper(CH),fromLast=TRUE)] >> CH >[1] "MRTZIt" "MRTZIT" "PPBZJG" "ppbzJG" >> chnew >[1] "MRTZIt" "MRTZIt" "PPBZJG" "PPBZJG" > > >However Bert's code is also working very well, except following scenario: > >> CH <- c("MRTZIt", "MRTZIT", "PPBZJG", "PPBZJG") >> caps <- CH %in% toupper(CH) >> noncaps <- CH[!caps] >> chnew <- CH >> chnew[caps] <- noncaps[match(CH[caps], toupper(noncaps))] >> CH >[1] "MRTZIt" "MRTZIT" "PPBZJG" "PPBZJG" >> chnew >[1] "MRTZIt" "MRTZIt" NA NA > > >In my case, both can be well possibilities. > >Any better pointer? > >Thanks and regards, > > > > > >On Sun, Aug 11, 2013 at 9:42 PM, Bert Gunter <gunter.ber...@gene.com> wrote: > >Well, maybe: it assumes that the uppercase string version always >>occurs after the nonuppercase version. That's why I rejected it. >> >>However, it points out something important: details matter. The more >>one knows about the nature of the problem, the better the solution one >>can tailor -- a remark for which the response should be, "well duhhh!" >> But posters frequently seem to disregard this. >> >>-- Bert >> >> >>On Sun, Aug 11, 2013 at 8:43 AM, arun <smartpink...@yahoo.com> wrote: >>> >>> >>> Hi, >>> >>> May be this helps: >>> chnew<-CH >>> chnew[duplicated(toupper(CH))]<-CH[duplicated(toupper(CH),fromLast=TRUE)] >>> chnew >>> #[1] "aBd" "sTb" "aBd" "dFDasd" "asd" "dFDasd" >>> A.K. >>> >>> >>> ----- Original Message ----- >>> From: Christofer Bogaso <bogaso.christo...@gmail.com> >>> To: r-help <r-help@r-project.org> >>> Cc: >>> Sent: Sunday, August 11, 2013 8:39 AM >>> Subject: [R] Working with string >>> >>> Hello again, >>> >>> Let say I have a lengthy character vector like: >>> >>> CH <- c("aBd", "sTb", "ABD", "dFDasd", "asd", "DFDASD") >>> >>> Now I want to create a vector like: >>> >>> CH_New <- c("aBd", "sTb", "aBd", "dFDasd", "asd", "dFDasd") ## the 3rd and >>> 6th element replaced >>> >>> Basically, the goal is: >>> >>> If an element has all upper case then it will find another element with all >>> lower case or mix of upper/lower case. Then the all-upper-case element will >>> be replaced by that mix. If there is multiple match then chose the first >>> one. >>> >>> >>> Can somebody give me any pointer how can I achieve that? >>> >>> Thanks and regards, >>> >>> [[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. >>> >>> >>> ______________________________________________ >>> 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. >> >> >> >> >>-- >> >>Bert Gunter >>Genentech Nonclinical Biostatistics >> >>Internal Contact Info: >>Phone: 467-7374 >>Website: >>http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm >> > ______________________________________________ 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.