Dikesh Faldu Dikesh Faldu - 1 year ago 75
R Question

R - How to compare two words sentence if order is not same?

Lets take example

z <- "Dikesh Faldu"
y <- "Faldu Dikesh"

I want to get process both of this variable and give me same output as "DikeshFaldu"

lets put another example

i have one variable that has all this values like

[1] dikesh faldu
[2] xyz abc
[3] faldu dikesh
[4] anything like
[5] but only
[6] two words
[7] only but


then i want output like

[1] dikeshfaldu
[2] xyzabc
[3] dikeshfaldu
[4] anythinglike
[5] butonly
[6] twowords
[7] butonly


[1] faldudikesh
[2] xyzabc
[3] faldudikesh
[4] anythinglike
[5] onlybut
[6] twowords
[7] onlybut

Answer Source

As per the inputs by @zx8754 , @Dirty Sock Sniffer and @RHertel, you can try,

sapply(strsplit(charvec, " "), function(x) paste(sort(x), collapse=""))

#[1] "dikeshfaldu"  "abcxyz"       "dikeshfaldu"  "anythinglike" "butonly"     
#[6] "twowords"     "butonly" 


charvec <- c("dikesh faldu", "xyz abc", "faldu dikesh", "anything like", 
              "but only", "two words", "only but")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download