Skip to content

map two names into one

6 messages · Tammy Ma, arun

#
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.
#
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.
#
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.
#
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.
#
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.