This article describes how to recode a variable, such as a state question, into...
... a variable of regions based on a lookup table or variables:
Requirements
- A lookup table with at least 2 columns, one to match on and one to use as the new recoded value. In this example, we will use a pasted table (Create > Tables > Enter Data) called US.States
- A variable in your data set to match to the new recoded values in the lookup table. This is the What state do you live in? variable in this example. Note that if your column to match on in the lookup table is all numbers, it will be converted to numeric data, and the variable in your data set must be Question Type > Number.
Method 1 - mapvalues() function
1. On the Variables and Questions tab and right click on a row to insert your variable and select Insert Variable(s) > R Variable.
2. In the bottom of the window, give it a Question Name, something like New Groups.
3. Paste the below under R CODE:
plyr::mapvalues(`What state do you live in?`, from=US.States$State, to=US.States$Region, warn_missing=F)
- The above code uses the R library plyr's mapvalues function so we need to first load that.
- The question, What state do you live in?, is in backticks because the label has spaces.
- The state variable is then compared to the State field from the US.States table and returns the Region field.
- Note, if your data does not match a lookup category, it will return the original data that your are looking up.
4. Click the play button to run the code and review the results.
5. Click Add R Variable.
6. Change the Question Type to Pick One.
Method 2 - match() function
1. On the Variables and Questions tab and right click on a row to insert your variable and select Insert Variable(s) > R Variable.
2. In the bottom of the window, give it a Question Name, something like New Groups.
3. Paste the below under R CODE:
x = as.character(`What state do you live in?`)
lookup = US.States
m = match(x,lookup$State)
x[!is.na(m)] = lookup[m[!is.na(m)], "Region"]
x[is.na(m)] = "Outside of USA"
x
- The above code converts our state question into text values and defines it as x.
- We use the match function to compare the state data with the lookup table's State field. This returns an index number.
- Where x matches the lookup table, we recode it with the corresponding Region data.
- Anything that does not match, i.e. is NA, we recode as "Outside of USA".
- We then return the recoded x as the final result.
4. Click the play button to run the code and review the results.
5. Click Add R Variable.
6. Change the Question Type to Pick One.
See Also
How to Work with Conditional R Formulas
How to Use R Code to Create a Filter Based on Single-Response Questions
How to Create a New Variable Based on Other Variables using R