Thanks. Yes. Your approach can identify: Glaxy ace S 5830 and S 5830 Glaxy ace But you can not identify using same program: Iphone 4S 16 G Iphone 4S 16G How should I solve both in same time. Kind regards,Tammy [[alternative HTML version deleted]]
HI, The context was missing.? I had to dig through Nabble to find what you actually meant. ?vec1<-"Iphone 4S 16 G" ?vec2<-"4S G 16 Iphone" ?vec3<-"16 G Iphone 4S" vec4<-"3S G 16 Iphone" ?res2<-paste(sort(unlist(strsplit(vec2," "))),collapse= " ") ?res3<-paste(sort(unlist(strsplit(vec3," "))),collapse= " ") ?res1<-paste(sort(unlist(strsplit(vec1," "))),collapse= " ") res4<-paste(sort(unlist(strsplit(vec4," "))),collapse= " ") ?identical(res1,res2) #[1] TRUE ?identical(res1,res3) #[1] TRUE identical(res1,res4) #[1] FALSE ?res1 #[1] "16 4S G Iphone" ?res4 #[1] "16 3S G Iphone" I think I previously used: res33<-paste(sort(unlist(strsplit(vec3," "))),collapse= "_") #this should also work ?res44<-paste(sort(unlist(strsplit(vec4," "))),collapse= "_") res11<-paste(sort(unlist(strsplit(vec4," "))),collapse= "_") ?identical(res11,res33) #[1] FALSE identical(res11,res44) #[1] TRUE A.K. ________________________________ From: Tammy Ma <metal_licaling at live.com> To: smartpink111 at yahoo.com; "r-help at r-project.org" <r-help at r-project.org> Sent: Thursday, November 8, 2012 8:12 AM Subject: Re: [R] map two names into one Thanks. Yes. Your approach can identify: Glaxy ace S 5830? and S 5830 Glaxy ace But you can not identify using same program: Iphone 4S 16 G Iphone 4S 16G How should I solve both in same time. Kind regards, Tammy????
Hi, There was a typo in my previous email. ?vec1<-"Iphone 4S 16 G" ?vec2<-"4S G 16 Iphone" ?vec3<-"16 G Iphone 4S" vec4<-"3S G 16 Iphone" res11<- paste(sort(unlist(strsplit(vec1," "))),collapse="_") # I typed vec4 instead of vec1 res33<-paste(sort(unlist(strsplit(vec3," "))),collapse= "_") res44<-paste(sort(unlist(strsplit(vec4," "))),collapse= "_") ?res11 #[1] "16_4S_G_Iphone" ?identical(res11,res33) #[1] TRUE ?identical(res11,res44) #[1] FALSE I am not sure what the problem is.? It works with this example.? If you can provide some details, then it would be more helpful. A.K. ________________________________ From: Tammy Ma <metal_licaling at live.com> To: smartpink111 at yahoo.com; "r-help at r-project.org" <r-help at r-project.org> Sent: Thursday, November 8, 2012 8:12 AM Subject: Re: [R] map two names into one Thanks. Yes. Your approach can identify: Glaxy ace S 5830? and S 5830 Glaxy ace But you can not identify using same program: Iphone 4S 16 G Iphone 4S 16G How should I solve both in same time. Kind regards, Tammy????
HI Tammy, Apologies! ? I failed to notice the space between 16 and G in your example.? Just to understand it, how often do you find these space issues?? Also, is it always between G and which ever number before that?? Do you find space issues between S and the number before that? If the problem is only for G, then try this: fun1<-function(x){ ?res<-ifelse(grepl("\\sG",x),x,gsub("(.*)(G)","\\1 \\2",x)) ?res2<-paste(sort(unlist(strsplit(res," "))),collapse= "_") ?res2} ?vec1<-"Iphone 4S 16 G" ?vec2<-"4S G 16 Iphone" ?vec3<-"16 G Iphone 4S" vec4<-"3S G 16 Iphone" vec5<-"Iphone 4S 16G" ? fun1(vec1) #[1] "16_4S_G_Iphone" ?fun1(vec2) #[1] "16_4S_G_Iphone" ?fun1(vec3) #[1] "16_4S_G_Iphone" ?fun1(vec4) #[1] "16_3S_G_Iphone" ?fun1(vec5) #[1] "16_4S_G_Iphone" ?identical(fun1(vec1),fun1(vec5)) #[1] TRUE ?identical(fun1(vec1),fun1(vec4)) #[1] FALSE A.K. ________________________________ From: Tammy Ma <metal_licaling at live.com> To: smartpink111 at yahoo.com; "r-help at r-project.org" <r-help at r-project.org> Sent: Thursday, November 8, 2012 8:12 AM Subject: Re: [R] map two names into one Thanks. Yes. Your approach can identify: Glaxy ace S 5830? and S 5830 Glaxy ace But you can not identify using same program: Iphone 4S 16 G Iphone 4S 16G How should I solve both in same time. Kind regards, Tammy????
HI, A small modification of my earlier approach as it creates a problem for "Glaxy ace..." Use: fun1<-function(x){ ? res<-ifelse(grepl("\\sG",x),x,gsub("(.*)(G)","\\1 \\2",x)) ? res2<-paste(sort(unlist(strsplit(res," "))),collapse= "") # collapse="" ?res2} ?vec6<-"Glaxy ace S 5830" ?vec7<-"S 5830 Glaxy ace" vec1<-"Iphone 4S 16 G" ? vec2<-"4S G 16 Iphone" ? vec3<-"16 G Iphone 4S" ?vec4<-"3S G 16 Iphone" ?vec5<-"Iphone 4S 16G" fun1(vec6) #[1] "5830aceGlaxyS" ?fun1(vec7) #[1] "5830aceGlaxyS" ?fun1(vec1) #[1] "164SGIphone" ?fun1(vec2) #[1] "164SGIphone" ?fun1(vec3) #[1] "164SGIphone" ?fun1(vec4) #[1] "163SGIphone" ?fun1(vec5) #[1] "164SGIphone" ?fun1(vec6) #[1] "5830aceGlaxyS" ?identical(fun1(vec6),fun1(vec7)) #[1] TRUE ?identical(fun1(vec5),fun1(vec1)) #[1] TRUE ?identical(fun1(vec1),fun1(vec4)) #[1] FALSE A.K. ________________________________ From: Tammy Ma <metal_licaling at live.com> To: smartpink111 at yahoo.com; "r-help at r-project.org" <r-help at r-project.org> Sent: Thursday, November 8, 2012 8:12 AM Subject: Re: [R] map two names into one Thanks. Yes. Your approach can identify: Glaxy ace S 5830? and S 5830 Glaxy ace But you can not identify using same program: Iphone 4S 16 G Iphone 4S 16G How should I solve both in same time. Kind regards, Tammy????
Hi, After thinking about this for some other situations, I think this function will be more suitable. fun2<-function(x){ res1<-toupper(x) res2<- gsub("^\\s+|\\s+$","",paste0(sort(unlist(strsplit(res1,""))),collapse="")) res2} vec6<-"Glaxy ace S 5830" ?vec7<-"S 5830 Glaxy ace" vec1<-"Iphone 4S 16 G" ? vec2<-"4S G 16 Iphone" ? vec3<-"16 G Iphone 4S" ?vec4<-"3S G 16 Iphone" ?vec5<-"Iphone 4S 16G" ?fun2(vec1) #[1] "146EGHINOPS" ?fun2(vec2) #[1] "146EGHINOPS" ?fun2(vec3) #[1] "146EGHINOPS" ?fun2(vec4) #[1] "136EGHINOPS" ?fun2(vec5) #[1] "146EGHINOPS" ?fun2(vec6) #[1] "0358AACEGLSXY" ?fun2(vec7) #[1] "0358AACEGLSXY" ?identical(fun2(vec1),fun2(vec5)) #[1] TRUE ?identical(fun2(vec1),fun2(vec4)) #[1] FALSE ?vec5 #[1] "Iphone 4S 16G" ?identical(fun2(vec6),fun2(vec7)) #[1] TRUE A.K. ________________________________ From: Tammy Ma <metal_licaling at live.com> To: smartpink111 at yahoo.com; "r-help at r-project.org" <r-help at r-project.org> Sent: Thursday, November 8, 2012 8:12 AM Subject: Re: [R] map two names into one Thanks. Yes. Your approach can identify: Glaxy ace S 5830? and S 5830 Glaxy ace But you can not identify using same program: Iphone 4S 16 G Iphone 4S 16G How should I solve both in same time. Kind regards, Tammy????