Skip to content

Instantly share code, notes, and snippets.

@AndrewLJackson
Created December 2, 2014 11:59
Show Gist options
  • Select an option

  • Save AndrewLJackson/d5a8735a77c13a8e60a2 to your computer and use it in GitHub Desktop.

Select an option

Save AndrewLJackson/d5a8735a77c13a8e60a2 to your computer and use it in GitHub Desktop.
Manually recode variables using a series of == statements
# 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