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

From: Ajay Narottam Shah <ajayshah_at_mayin.org>
Date: Mon 09 May 2005 - 17:01:08 EST


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@mayin.org                      Department of Economic Affairs
http://www.mayin.org/ajayshah           Ministry of Finance, New Delhi

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Mon May 09 17:06:12 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:39 EST