-1

I have a dataframe key and a column in a dataframe that needs to be remapped according to the key.

IDhex=c("asdj23","kjh23s")
IDHuman=c("Label1","Label2")

IDKey=data.frame(IDhex,IDHuman)

MasterDF$UserID has a thousands of entries of either asdj23, kjh23s or '' (empty). I need to find an replace this column according to the mapping from IDkey. I need a coding solution that is INDEPENDENT OF IDKEY size. There will be a loop that takes in different keys that are 2xn in size.

  • 1
    What do you mean by "remaped?" You should provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data and the desired output. – MrFlick Mar 11 '16 at 23:58
  • Make a new data frame, a look-up table. Then `merge` or `left_join`. – Gregor Thomas Mar 12 '16 at 00:01
  • Typo: `IDHuman != IDhuman`. And IDKey is not a dataframe. – vaettchen Mar 12 '16 at 00:05

1 Answers1

0

I'm not entirely sure what you are trying to do, but you could look at gsub() which lets you search using a pattern or regex and replace with a designated value.

For instance:

x <- rep(c("a","b"), 10)
x

[1] "a" "b" "a" "b" "a" "b" "a" "b" "a" "b" "a" "b" "a" "b" "a" "b" "a" "b" "a" "b"

match_pattern <- "a"
replace_pattern <- "fixed"
gsub(match_pattern, replace_pattern, x)

[1] "fixed" "b" "fixed" "b" "fixed" "b" "fixed" "b" "fixed" "b" "fixed" "b" "fixed" "b"
[15] "fixed" "b" "fixed" "b" "fixed" "b"

gfgm
  • 3,627
  • 14
  • 34