Created
April 16, 2012 20:50
-
-
Save geofferyzh/2401425 to your computer and use it in GitHub Desktop.
RinAction - R Data Manipulation - recode variables
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
################################################# | |
## Recoding variables ## | |
################################################# | |
# Recoding categorical variable | |
leadership$agecat[leadership$age > 75] <- "Elder" | |
leadership$agecat[leadership$age > 45 & | |
leadership$age <= 75] <- "Middle Aged" | |
leadership$agecat[leadership$age <= 45] <- "Young" | |
# Using within() function (better way) | |
leadership <- within(leadership, { | |
agecat <- NA | |
agecat[age > 75] <- "Elder" | |
agecat[age >= 55 & age <= 75] <- "Middle Aged" | |
agecat[age < 55] <- "Young" | |
}) | |
# recode() function in the "car" package |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment