Conditional replacement of string with another string

前端 未结 2 2003
我寻月下人不归
我寻月下人不归 2021-01-20 14:39

I have the following structure of the data with all variables being in the string format:

    v1      v2      c1      c2           c1c2
00035A  943567  00088         


        
2条回答
  •  离开以前
    2021-01-20 15:29

    There is also an alternative approach using update in a self-join

    library(data.table)
    #coerce to data.table
    setDT(df)[
      # 1st self join & update
      df, on = .(v1 = c1), v1 := c1c2][
        # 2nd slef join & update
        df, on = .(v2 = c2), v2 := c1c2][]
    
                   v1            v2     c1     c2          c1c2
     1:        00035A        943567 00088E 63968E 00088E;63968E
     2: 00088E;63968E 00088E;63968E 00088E 63968E 00088E;63968E
     3: 00088E;63968E        925524 00088E 63968E 00088E;63968E
     4:        000361        237924 00088E 63968E 00088E;63968E
     5:        000361        83367A 00088E 63968E 00088E;63968E
     6:        00055X        49328R 00088E 63968E 00088E;63968E
     7:        00056N        87885Q 00088E 63968E 00088E;63968E
     8:        000794        69911G 00088E 63968E 00088E;63968E
     9:        23792A        001674 00088E 63968E 00088E;63968E
    10:        63968E        17275R 00088E 63968E 00088E;63968E
    

    Caveat

提交回复
热议问题