[R] Need a factor level even though there are no observations
falissard
falissard_b at wanadoo.fr
Sun May 8 17:23:27 CEST 2005
Hello,
Does this work?
m <- as.factor(education.man)
levels(m)[1:3] <- factorlabels
table(m)
Bruno
----------------------------------------------------------------------------
Bruno Falissard
INSERM U669, PSIGIAM
"Paris Sud Innovation Group in Adolescent Mental Health"
Maison de Solenn
97 Boulevard de Port Royal
75679 Paris cedex 14, France
tel : (+33) 6 81 82 70 76
fax : (+33) 1 45 59 34 18
web site : http://perso.wanadoo.fr/bruno.falissard/
----------------------------------------------------------------------------
-----Message d'origine-----
De : r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] De la part de Ajay Narottam Shah
Envoyé : dimanche 8 mai 2005 15:57
À : r-help
Objet : [R] Need a factor level even though there are no observations
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 at mayin.org Department of Economic Affairs
http://www.mayin.org/ajayshah Ministry of Finance, New Delhi
______________________________________________
R-help at 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
More information about the R-help
mailing list