r - Replace value from other dataframe -
i have data frame (x) factor variable has values seperated comma. have data frame (y) description same values. want replace values in data frame (x) description data frame (y). highly appreciated.
say example, 2 data frame looks 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 2 5 b 3 45 c 4 35 d i need output below
s.no x 1 a,b,c 2 d,b c c
we can split 'x' column in 'x' dataset ',', loop on list, match value 'x' column in 'y' numeric index, corresponding 'description' value 'y' , paste together.
x$x <- sapply(strsplit(x$x, ","), function(z) tostring(y$description[match(as.numeric(z), y$x)])) x # s.no x #1 1 a, b, c #2 2 d, b #3 3 c note: if 'x' column in 'x' factor class, use strsplit(as.character(x$x, ","))
Comments
Post a Comment