r - Looking for a more concise way to recategorise a variable -
i have vector of integer ages want turn multiple categories:
ages <- round(runif(10, 0, 99))
now want variable binned 3 categories, depending on age. want output object, ages.cat
this:
young mid old 1 0 0 1 2 1 0 0 3 1 0 0 4 1 0 0 5 1 0 0 6 0 1 0 7 1 0 0 8 0 0 1 9 0 1 0 10 0 1 0
at present creating object following code:
ages.cat <- array(0, dim=c(10,3)) # create categorical object 3 bins ages.cat[ages < 30, 1] <- 1 ages.cat[ages >= 30 & ages < 60, 2] <- 1 ages.cat[ages >= 60, 3] <- 1 ages.cat <- data.frame(ages.cat) names(ages.cat) <- c("young", "mid", "old")
there must faster , more concise way recode data - had play dplyr couldn't see solution particular problem functions. ideas? what's 'canonical' solution problem in base r or using package? whatever alternatives, i'm they'll more concise clunky code!
its 2 one-liners.
use cut
create factor:
ages <- round(runif(10, 0, 99)) agef=cut(ages,c(-inf,30,60,inf),labels=c("young","mid","old")) > agef [1] young mid young young old mid old young old old levels: young mid old
usually you'd leave factor , work it, if using r's modelling functions they'll work out matrix you. if doing yourself:
use model.matrix
create matrix, -1 remove intercept , create columns each level:
> m = model.matrix(~agef-1) > m agefyoung agefmid agefold 1 1 0 0 2 0 1 0 3 1 0 0 4 1 0 0 5 0 0 1 6 0 1 0 7 0 0 1 8 1 0 0 9 0 0 1 10 0 0 1 attr(,"assign") [1] 1 1 1 attr(,"contrasts") attr(,"contrasts")$agef [1] "contr.treatment"
you can ignore contrasty stuff @ end, matrix attributes modelling.
Comments
Post a Comment