Skip to content

leeper/mcode

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

# Merge and recode across multiple variables Recoding data to shape it into a form suitable for analysis is one of the first, most important, and most time-consuming parts of data analysis. Data rarely come in precisely the form needed to analyze and this is especially true for social science data such as that generated by surveys, experiments, and registry data. This package provides tools for simplifying some aspects of recoding, to expand upon the set of tools provided by base R such as `cut`, `interaction`, `[<-`, `ifelse`, etc. and functions available in add-on packages such as the `recode` from [**car**](http://cran.r-project.org/web/packages/car/), `mapvalues` and `revalue` from [**plyr**](http://cran.r-project.org/web/packages/plyr/), etc. ## Installation ## [![travis-ci](https://travis-ci.org/leeper/mcode.svg)](https://travis-ci.org/leeper/mcode) To install the latest development version of **mcode** from GitHub: ```R if(!require("devtools")){ install.packages("devtools") library("devtools") } install_github("leeper/mcode") ``` ## Current Functionality ## So far the package includes four functions: `branch` and `unbranch`, `mergeNA`, and `mcode`. ### `branch` and `unbranch` ### The `branch` function provides a generalization of a common expression: `model.matrix(~0 + x, data = dataset)`. That expression creates a matrix of indicator (dummy) variables from a categorical vector. `branch` generalizes this so that it is possible to branch a vector of values into multiple vectors based on any arbitrarily branching rule. ```R a <- sample(1:5, 20, TRUE) b1 <- sample(1:2, 20, TRUE) b2 <- sample(1:2, 20, TRUE) branch(a, b1) # 2-column matrix branch(a, list(b1, b2)) # 4-column matrix ``` `unbranch` reverses this process by taking a multi-column matrix, or a data.frame, or a list of vectors, or some combination thereof, and collapsing them into a single vector. It inverts the behavior of `branch`: ```R a <- sample(1:5, 20, TRUE) b1 <- sample(1:2, 20, TRUE) b2 <- sample(1:2, 20, TRUE) b <- branch(a, list(b1, b2)) u <- unbranch(b) all.equal(a, u) ``` `unbranch` is especially useful for extracting relevant categories out of two or more vectors to create a single vector that ignores all irrelevant (replacing them with a specified value): ```R m1 <- c(1,3,4,5,2) m2 <- c(0,2,2,2,4) # replace irrelevant categories with 0 unbranch(m1, m2, .ignore = c(1,2,3)) # replace irrelevant categories with NA unbranch(m1, m2, .ignore = c(1,2,3), .fill = NA) ``` ### `mergeNA` ### `mergeNA` provides a wrapper for `unbranch` that sets different default values. This makes it useful for mergining multiple vectors (or vectors from data.frames and lists, or columns from matrices) into a single vector. It works on vectors that have "mutually exclusive missingness" (i.e., the vectors all have the same length and many missing values such that for each position (in each vector) only one vector has a valid, non-`NA` value. This is especially useful for recoding complex survey questionnaire and experimental data, which often stores values to the same variable in different vectors within a data.frame (e.g., because survey respondents in each treatment condition were stored in their own data.frame column). For example: ```R mergeNA(c(1,2,NA,NA,NA), c(NA,NA,NA,4,5)) # [1] 1 2 NA 4 5 ``` This is especially useful when it is necessary to merge a potentially large number of variables (e.g., from branched survey questions) into a single variable. ### `mcode` `mcode` is still experimental. Building on `car::recode`, the function aims to streamline recoding of variables in potentially complex ways. Where `car::recode` converts an input vector into an output vector following a set of recoding rules, `mcode` aims to recode an arbitrary number of vectors into a single output vector. For example one may want to create a categorical variable representing age and gender categories. Normally this would require two calls to `recode` and/or a long set of additional operations (e.g., `cut`, `[<-`, `ifelse`, `*`, `+`, and/or `interaction`). `mcode` will consolidate these steps into a single operation. ```R a <- c(1,2,1,2,1,NA,2,NA) b <- c(1,1,2,2,NA,1,NA,2) # recode using `mcode` m1 <- mcode(a, b, recodes = "c(1,1)=1;c(1,2)=2;c(2,1)=3;c(2,2)=4") # compare to `ifelse`: m2 <- ifelse(a == 1 & b == 1, 1, ifelse(a == 1 & b == 2, 2, ifelse(a == 2 & b == 1, 3, ifelse(a == 2 & b == 2, 4, NA)))) identical(m1, m2) # compare to a sequence of extraction statements m3 <- rep(NA, length(a)) m3[a == 1 & b == 1] <- 1 m3[a == 1 & b == 2] <- 2 m3[a == 2 & b == 1] <- 3 m3[a == 2 & b == 2] <- 4 identical(m1, m3) # compare to interaction m4 <- interaction(a, b) levels(m4) <- c("1.1" = 1, "1.2" = 2, "2.1" = 3, "2.2" = 4)[levels(m4)] m4 <- as.numeric(as.character(m4)) identical(m1, m4) ``` 

About

Functions to merge and recode across multiple variables

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages