Working with data values (dictionary). dataframes and recoding in R

I am trying to work with a data.frame that contains 30 or so variables. These variables all have different numeric values (e.g., "0" or "32"). The numeric values map to strings (e.g., in variable Q1, "0" is "Urban"; in variable Q6, "32" is "Not available").

I saw some uses of recode online, and an older plyr package that did this mapping, as well as the match function, but I didn't find anything that matches the exact structure I have.

I provide a reproducible example below:

  test <-"1", "2", "3"))
  colnames(test) <- "Q1"
  dictionary <-"1", "2", "3"))
  dictionary$values <- c("dog", "fish", "cat")
  dictionary$question <- c("Q1", "Q1", "Q1")
  colnames(dictionary)[1] <- "keys"

So, here, dictionary$question contains the question to map to; and then the "keys" and "values" provide the mapping. So, in the test dataframe, I have one variable (Q1) that takes three possible values, "1", "2", or "3". I need to map these back to "dog", "fish", "cat".

But I need an automatic way to map the column in the target dataframe to the rows in the dictionary, and then translate the values, since in practice I have over 1000 possible values and 30 variables.

Read more here:

Content Attribution

This content was originally published by jpugliese at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: