I want to apply strsplit in such a fashion that if there exist an identical pair of values with & (e.g. this is one pair with & (NINA & SAM)
) and also with | (e.g. this is another pair but with | (NINA | SAM)
) then keep the one with &
Below are 2 possible cases, and the length of these vectors (vec1, vec2) might vary among actual cases.
Case 1
> vec1
[1] "((PAUL & SAM) | (PAUL | SAM) | (NINA & SAM) | (NINA | SAM) | (NINA & PAUL) | (NINA | PAUL))"
> vec2
[1] "((PAUL | SAM) & (PAUL & SAM) & (NINA | SAM) & (NINA | PAUL) & (NINA & PAUL) & (NINA & SAM))"
Case 2
> vec1
[1] "((PAUL | SAM) | (PAUL & SAM) | (!NINA & SAM) | (!NINA & PAUL))"
> vec2
[1] "((PAUL | SAM) & (PAUL & SAM) & (!NINA & SAM) & (!NINA & PAUL))"
This should be the outputs:
Case 1
> vec1
[1] "((PAUL & SAM) | (NINA & SAM) | (NINA & PAUL))"
> vec2
[1] "((PAUL & SAM) & (NINA & PAUL) & (NINA & SAM))"
Case 2
> vec1
[1] "((PAUL & SAM) | (!NINA & SAM) | (!NINA & PAUL))"
> vec2
[1] "((PAUL & SAM) & (!NINA & SAM) & (!NINA & PAUL))"
What I have tried so far:
My idea was to first remove the ((
and ))
from the start and end of the vector then split the vec1 on ") | (
" and vec2 on ") & (
" . Then further split the indices on space*space
and check if sub index 1 and 2 matches with any other sub-index, if yes then keep the one which has &
. Then put everything back together. I have a limited knowledge of R and I was unable to implement what I have in my mind. Any help will be much appreciated!