Created
December 2, 2014 11:59
-
-
Save AndrewLJackson/d5a8735a77c13a8e60a2 to your computer and use it in GitHub Desktop.
Manually recode variables using a series of == statements
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # example code to reclassify existing coded data | |
| # some data | |
| mydata <- data.frame(orig.codes = c(1,1,1,2,2,3,4,4,5,6,7,7,7,8,8,9,10,10,10)) | |
| # a blank vector of NAs into which we will place the new codes | |
| mydata$new.codes <- NA * numeric(length(mydata$orig.codes)) | |
| # you could do this as an automated loop, but this is the manual way | |
| # recode mappings will go from old to new as follows | |
| # 1, 2, 3 --> 1 | |
| # 4, 5 --> 2 | |
| # 6, 7, 8 --> 3 | |
| # 9, 10 --> 4 | |
| mydata$new.codes[mydata$orig.codes==1] <- 1 | |
| mydata$new.codes[mydata$orig.codes==2] <- 1 | |
| mydata$new.codes[mydata$orig.codes==3] <- 1 | |
| mydata$new.codes[mydata$orig.codes==4] <- 2 | |
| mydata$new.codes[mydata$orig.codes==5] <- 2 | |
| mydata$new.codes[mydata$orig.codes==6] <- 3 | |
| mydata$new.codes[mydata$orig.codes==7] <- 3 | |
| mydata$new.codes[mydata$orig.codes==8] <- 3 | |
| mydata$new.codes[mydata$orig.codes==9] <- 4 | |
| mydata$new.codes[mydata$orig.codes==10] <- 4 | |
| # check that my data is correct by inspecting its content at the | |
| # command window | |
| mydata | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment