Last active
August 29, 2015 14:07
-
-
Save kbroman/6bc9e1c63eee7f17accc to your computer and use it in GitHub Desktop.
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
# convert c("female", "Male") to c(0,1) | |
# magrittr is way nicer than nested function calls | |
convert_sexcodes <- | |
function(codes) | |
{ | |
require(magrittr) | |
tolower(codes) %>% | |
substr(., 1, 1) %>% | |
match(., c("f", "m")) - 1 | |
} | |
convert_sexcodes_old <- | |
function(codes) | |
{ | |
match(substr(tolower(codes), 1, 1), c("f", "m")) - 1 | |
} | |
Or: car::recode(codes, "'Female'=0;'Male'=1")
Or revalue()
from (d)plyr
:
x <- c('Female', 'male', 'Male', 'female')
x %>% tolower %>% revalue(c(female = 0, male = 1)) %>% as.numeric
Thanks to you all!
Base R has a lot of good functions.
codes <- ifelse(codes == "Male", 1, 0)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pipestravaganza: