[R] problems with categorical variables

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Mon Sep 8 14:58:06 CEST 2003


arinbasu at softhome.net writes:

> Hi All: I am working on a dataset of a study on healthcare workers.
> One of the variables I am studying is a categorical variable (variable
> name:EDUC, indicates educational achievement, with 6 levels:
> "illiterate", "primary", "junior high school", "high school
> completed", "undergraduate", and "postgraduate"). I want to collapse
> the 6 levels to a 4-level categorical variable (let's call it
> "educrec", with the following levels: "postgraduate", "undergraduate",
> "some schooling" and "illiterate"). I initially tried the following
> code: x <- table(EDUC)
> educrec <- c(x[1], x[2], (x[3]+x[4]+x[5]), x[6])) table(educrec) gives
> me the tally count just fine, but I cannot use educrec in any cross
> tabulation. Would greatly appreciate if you can advise on the correct
> way to recode categorical variables to categorical variables in R.
> I use Windows XP (Home), R-version 1.7.1
> I looked for solutions in the "introduction to R" and the archives,
> but wasn't successful. -Arin Basu

EDUC2 <- EDUC
levels(EDUC2) <- c("postgraduate", 
                   "undergraduate",
                   "some schooling", 
                   "some schooling", 
                   "some schooling", 
                   "illiterate")

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list