user6489260
user6489260

Reputation:

Replace string with respective matching one

I need to replace the strings in df2 with the partially matching string in df1 .

Like 1MG Solution has 1MG in df1 as well thus need to replace it with 1MG from df1 . This needs to be done for million records .

  > df1
          A       
        1MG             
        ABOF          
        Amazon        
        American Swan
        Clovia       

    > df2


      A                 B                                          
1MG Solution           1MG
ABOF Prime             ABOF
Dual Command           NA
Amazon AWF             AMazon
American Swan Fi       AMerican Swan
Clovia World Spaces    Clovia
Shape Makers           NA
Unions                 NA

Upvotes: 0

Views: 60

Answers (2)

Sotos
Sotos

Reputation: 51582

stringr package is ideal for such operations,

library(stringr)
df2$B <- str_extract(df2$A, paste(df1$A, collapse = '|'))
df2
#                    A             B
#1        1MG Solution           1MG
#2          ABOF Prime          ABOF
#3        Dual Command          <NA>
#4          Amazon AWF        Amazon
#5    American Swan Fi American Swan
#6 Clovia World Spaces        Clovia
#7        Shape Makers          <NA>
#8              Unions          <NA>

Note: You will need to convert your variables to character( If they are factors)

Upvotes: 1

Christophe D.
Christophe D.

Reputation: 1089

By using grep function you can find if any of the pattern in the first dataframe is present in a second.
For example :

df <- data.frame(A=c("1MG","ABOF","Amazon","American Swan","Clovia"))
label=c("1MG Sol","ABOF Prim",
"Dual Command","Amazon AWF","something else",
"American Swan Fi","Clovia World Spaces")
apply(df,1,function(x) grep(x,label)) #will return the matching postion

The apply function will return the matching position :

> apply(df,1,function(x) grep(x,label))
[1] 1 2 4 6 7

I think you could find a solution in this way

Upvotes: 0

Related Questions