[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 40 Wife 3 32 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 40 Wife 3 32 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 [EMAIL PROTECTED] 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
RE: [R] Need a factor level even though there are no observations
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 : [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 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 40 Wife 3 32 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 40 Wife 3 32 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 [EMAIL PROTECTED] 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 __ 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
Re: [R] Need a factor level even though there are no observations
Set levels not labels in the factor call. E.g. factor(School, levels = factorlabels) On Sun, 8 May 2005, Ajay Narottam Shah wrote: 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 40 Wife 3 32 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 40 Wife 3 32 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 [EMAIL PROTECTED] 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 -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ 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