[R] Need a factor level even though there are no observations

From: Ajay Narottam Shah <ajayshah_at_mayin.org>
Date: Sun 08 May 2005 - 23:56:55 EST


I'm in this situation:

     factorlabels <- c("School", "College", "Beyond")

with data for 8 families:

     education.man  <- c(1,2,1,2,1,2,1,2)       # Note : no "3" values
     education.wife <- c(1,2,3,1,2,3,1,2)       # 1,2,3 are all present.

My goal is to create this table:

                     School     College      Beyond
       Husband       4          4            0
       Wife          3          3            2


How do I do this?

I can readily do:

     education.wife <- factor(education.wife, labels=factorlabels)

But this breaks:

     education.man <- factor(education.man, labels=factorlabels)

because none of the families have a husband who went beyond college.

I get around this problem in a limited way by:

     cautiously <- function(x, labels) {
       factor(x, labels=factorlabels[as.numeric(levels(factor(x)))])
     }
     education.man <- cautiously(education.man, labels=factorlabels)

Now I get:

> table(education.man)

     School College 
          4       4 

> table(education.wife)
School College Beyond 3 3 2

This is a pain because now the two tables are not conformable. How do I get to my end goal, which is the table:

                     School     College      Beyond
       Husband       4          4            0
       Wife          3          3            2

In other words, how do I force education.man to have a factor with 3 levels - "School" "College" "Beyond" - even though there is no observation in "Beyond".

-- 
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 01:06:45 2005

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