Arrebimbomalho
Arrebimbomalho

Reputation: 186

Removing elements based on a partial string match between 2 data frames:

Suppose I have two data frames with strings in their elements:

B <- data.frame(c("abcd1","cdbax2","acdb3"))
colnames(B) <- "Strings"

A <- data.frame(c("abcd_11","cdba_12"))
colnames(A) <- "Strings"

yielding the pair of dataframes,

> A
  Strings
1    abcd_11
2    cdba_12

and

> B
  Strings
1   abcd1
2   cdbax2
3   acdb3

Desired output: B*, B purged of the partial strings that are not in A:

> B*
      Strings
    1   abcd1
    2   cdbax2

Any ideas will be much appreciated.

Cheers

Edit: a solution based on string length does not work, since it is possible to have differing lengths in the dataframe

Upvotes: 0

Views: 37

Answers (1)

Onyambu
Onyambu

Reputation: 79358

 B[max.col(-adist(A$Strings,B$Strings)),]
[1] abcd1  cdbax2

Upvotes: 2

Related Questions