获取因子变量中级别和标签之间的原始关联 [英] Get original association between levels and labels in factor variables
问题描述
我正在寻找一个函数来获取因子变量的原始映射表.我导入一个Rdata文件.我有一个名为"FactVar"的因子变量.我知道"FactVar"的映射表如下:
I'm looking for a function to get the original mapping table of factor variable. I import an Rdata file. I've got a factor variable named "FactVar". I know the mapping table for "FactVar" is as follows:
"010025" -> city1
"015146" -> city2
"048017" -> city3
"082053" -> city4
在我的数据框中,"FactVar"数据如下(前5种情况):
In my dataframe the "FactVar" data are as follows (first 5 cases):
1: city1
2: city3
3: city4
4: city1
5: city3
因此,我的df中没有"city2".我可以使用哪个函数来获取原始映射表?它在我的Rdata文件中可用吗?
So, no "city2" in my df. Which function can I use to get the original mapping table? is it available in my Rdata file?
谢谢
我试图用一个更好的例子来澄清我的问题.我有一个调查问题,可能有以下答案:
I try to clarify my question with a better example. I have a survey question with the following possible answers:
1: "Yes"
2: "No"
8: "Don't Know"
9: "Not Applicable"
我创建一个因子变量"FactVar":
I create a factor variable "FactVar":
Var <- c(1,2,1,2,2,2,1,8,1,2)
FactVar <- factor(Var, levels=c(1,2,8,9), labels=c("Yes", "No", "Don't Know", "Not Applicable")
如您所见,在我的Rdata文件中,我有一个因子变量,其中没有数据链接到不适用"级别.如何获得我的调查问题中的原始映射表?
As you see, in my Rdata file I've got a factor variable where no data are linked to the level "Not Applicable". How can I get the original mapping table as in my survey question?
推荐答案
I had this question before, which was answered here: How to access actual internal factor lookup hashtable in R
对不起,我没有足够的声誉来发表评论.
Sorry I don't have enough reputation to put this in comments.
这篇关于获取因子变量中级别和标签之间的原始关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!