I have a data frame with many columns of integer variables, each representing a particular question on a food frequency questionnaire. I want to recode these columns to numerical variables representing the number of servings of that food eaten per month. mapvalues()
in plyr
seems like the right function, but I keep getting warning messages because not all of the foods have the full range of responses recorded:
The following `from` values were not present in `x`: 4, 5, 6, 7, 8
Setting warn_missing = false
does not return warning messages, but my data are still not being recoded.
Sample code (nails is my data frame, and foodnames is a list of variable names):
for (food in foodnames[c(7:81, 95:97)]){
mapvalues(eval(parse(text=paste("nails$", food))), from = c(1:8),
to = c(0, 1, 2.5, 4, 8, 14, 22, 28)), warn_missing = FALSE)
}
How can I get R to recode these variables even if they do not contain all the integers from 1 to 8? Thanks in advance.