I have
x<-c('abczzzdef','abcxxdef')
I want a function
fn(x)
that returns a length 2 vector
[1] 'zzz' 'xx'
How?
(I have tried searching for an answer but search terms like 'partial matching' give me something quite different)
Update
'length 2 vector' means length(fn(x)) is 2 and fn(x)[1] give "zzz" while fn(x)[2] gives "xx".
After trying out the answers provided, I realize I haven't been specific enough.
- There will only be 2 strings (in a vector) that I am comparing.
- The location of the different parts (zzz and xx) can be anywhere in the string. i.e. it could be
x<-c('zzzabcdef','xxabcdef')or it could be at the end. But the 2 strings are always at the same respective place (i.e. both at the beginning, or both at the middle, or both at the end). zzzandxxare obviously generic names. They could be different things (numbers, alphabet, symbols) and of different length (not necessarily 3 and 2).- Same comment applies to
abcanddef.
I have got some test cases
x1<-c('abcxxxttt','abczzttt')
x2<-c('abcxxxdef','abczz126gsdef')
x3<-c('xx_x123../t','z_z126gs123../t')
fn(x1) should give "xxx" "zz"
fn(x2) should give "xxx" "zz126gs"
fn(x3) should give "xx_x" "z_z126gs"
returns a length 2 vectormean? What would you expect'abczzzdefgggklmmmnto give??intersect?.strsplityour "x" and, then, collect the elements that are not in theintersection of the two splitted strings.