[R] Summary: My question about factor levels versus factor labels.

Ajay Narottam Shah ajayshah at mayin.org
Mon May 9 09:01:08 CEST 2005


Yesterday, I had asked for help on the list. Brian Ripley and Bruno
Falissard had most kindly responded to me. Here is the solution.

  > factorlabels <- c("School", "College", "Beyond")
  > #                       1          2         3

  > education.man  <- c(1,2,1,2,1,2,1,2)  # PROBLEM: Level "3" doesn't occur.
  > education.wife <- c(1,2,3,1,2,3,1,2)

  > education.wife <- factor(education.wife, labels=factorlabels)  # Is fine.

  > # But this breaks --
  > # education.man <- factor(education.man,   labels=factorlabels)

  > # Solution --
  > education.man <- factor(education.man, levels = c(1,2,3),
                                           labels=factorlabels)

  > # So now we can do --
  > a <- rbind(table(education.wife), table(education.man))
  > rownames(a) <- c("Wife", "Man")
  > print(a)
       School College Beyond
  Wife      3       3      2
  Man       4       4      0

which was the table that I had wanted.

-- 
Ajay Shah                                                   Consultant
ajayshah at mayin.org                      Department of Economic Affairs
http://www.mayin.org/ajayshah           Ministry of Finance, New Delhi




More information about the R-help mailing list