Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace value from other dataframe

Tags:

replace

r

dplyr

I have a data frame (x) with a factor variable which has values seperated by comma. I have another data frame (y) with description for the same values. Now I want to replace the values in the data frame (x) with the description from the data frame (y). Any help would be highly appreciated.

say for example, the two data frame looks like below

data frame (x)
    s.no    x
    1       2,5,45
    2       35,5
    3       45

data fram (y)
s.no    x   description
1       2      a
2       5      b
3       45     c
4       35     d

I need the output as below

s.no    x
1      a,b,c
2      d,b
c       c
like image 983
ssan Avatar asked Mar 10 '26 05:03

ssan


1 Answers

With splitstackshape:

library(splitstackshape)

cSplit(x, 'x', ',', 'long')[setDT(y), on='x'][,.(x=paste(description, collapse=',')), s.no]

#   s.no     x
#1:    1 a,b,c
#2:    2   b,d
#3:    3     c
like image 57
Colonel Beauvel Avatar answered Mar 11 '26 18:03

Colonel Beauvel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!