Skip to content
christophergandrud edited this page Jul 11, 2012 · 4 revisions

Sometimes we want to recode our variables. For example, imagine that we downloaded a data file of countries' executive regime type. We want to know if a country has a presidential or parliamentary system. Imagine the data we downloaded looked like this:

country regime
United Kingdom 2
United States 1

In this data the countries with presidential regimes are coded as 1 in the regime variable, Parliamentary systems are coded with a 2. What if we want the words presidential and parliamentary in our variable instead of 1 and 2?

There are a number of ways to recode variables like this in R (see this page at the Quick-R guide). One relatviely straightforward way to recode variables is using subscripts ([]). First quickly make our example data set:

country <- c("United Kingdom", "United States")
regime <- c(2, 1)
RegimeType <- data.frame(country, regime, stringsAsFactors = FALSE)

Now we have this data set:

RegimeType
##          country regime
## 1 United Kingdom      2
## 2  United States      1

To recode the regime variable with subscripts type:

RegimeType$regime[RegimeType$regime == 1] <- "presidential"
RegimeType$regime[RegimeType$regime == 2] <- "parliamentary"

Note: if you're wondering what the $ are for see bottom of the Basic Data in R: Objects and Dataframes wiki page.

Now our data looks like this:

RegimeType
##          country        regime
## 1 United Kingdom parliamentary
## 2  United States  presidential
Clone this wiki locally