r -case_when()基于多个现有变量的条件创建一个新变量
我正在努力创建一个名为“ edu_category”的新变量,以指示每个人是否经历了女性超人(Wive的教育水平<丈夫的教育水平),女性同性恋(Wive's Education Level ==丈夫)或女性降临(Wive的教育水平>丈夫的教育水平;丈夫的教育水平; )。
我的数据看起来像这样(女性== 1表示这个人是女性,0表示男性):
persyid | 女性 | 教育 | 配偶配偶 | 教育 |
---|---|---|---|---|
101 | 1 | 3 | 102 | 4 |
102 | 0 | 4 | 101 | 3 |
103 | 1 | 2 | 104 | 2 |
104 | 2 104 0 | 2 | 2 104 | 103 2 |
105 | 0 | 5 | 106 | 6 |
106 | 1 | 6 | 105 | 5 |
我希望创建一个新变量,以使我的数据看起来像这样:
persyid | 女性 | 教育 | 配偶配偶 | 教育 | edu_category |
---|---|---|---|---|---|
101 1 | 1 1 | 3 | 102 | 4 | 毛磨 |
102 | 0 | 4 | 101 | 3 | 杂质 |
103 | 1 | 2 | 104 | 2 | fhomogamy |
104 | 0 | 2 | 103 | 2 | fhomogamy |
105 | 0 | 5 | 106 | 6 | fhypogamy |
106 | 1 | 6 | 105 | 5 | fhypypogamy |
在这里== 0)教育水平为5,他的配偶(第106人)的教育水平为6,所以这是女性迟来,Wive的教育>丈夫(默认情况下,每个人的配偶都是异性的)。
现在,让我们看一下ID“ 106”的人,因为她是105人的配偶,我们还用相同的“ fhypogamy”填充了变量“ edu_category”。因此,从本质上讲,我们正在研究每个夫妻。
我尝试的是:
df2 <- df1 %>%
mutate(edu_category = case_when((SpouseEducation > EducationLevel) | (Female == 1) ~ 'FemaleHypergamy',
(SpouseEducation == EducationLevel) | (Female == 1) ~ 'FemaleHomogamy',
(SpouseEducation < EducationLevel) | (Female == 1) ~ 'FemaleHypogamy',
(SpouseEducation > EducationLevel) | (Female == 0) ~ 'FemaleHypogamy',
(SpouseEducation == EducationLevel) | (Female == 0) ~ 'FemaleHomogamy',
(SpouseEducation < EducationLevel) | (Female == 0) ~ 'FemaleHypergamy'))
但是,它并没有给出我的准确结果 - 变量“ edu_category”本身是成功创建的,而是“女性hyhypergamy”,“ amemhomogamy”和“ emagenhypogamy”并没有反映准确的情况。
我应该怎么办?谢谢您的帮助!
I am struggling to create a new variable named "edu_category" to indicate whether each person experiences Female Hypergamy (wive's education level < husband's), Female Homogamy (wive's education level == husband's), or Female Hypogamy (wive's education level > husband's).
My data looks like this (Female == 1 indicates this person is female, 0 indicates male):
PersonID | Female | EducationLevel | SpouseID | SpouseEducation |
---|---|---|---|---|
101 | 1 | 3 | 102 | 4 |
102 | 0 | 4 | 101 | 3 |
103 | 1 | 2 | 104 | 2 |
104 | 0 | 2 | 103 | 2 |
105 | 0 | 5 | 106 | 6 |
106 | 1 | 6 | 105 | 5 |
I wish to create a new variable so that my data looks like this:
PersonID | Female | EducationLevel | SpouseID | SpouseEducation | edu_category |
---|---|---|---|---|---|
101 | 1 | 3 | 102 | 4 | FHypergamy |
102 | 0 | 4 | 101 | 3 | FHypergamy |
103 | 1 | 2 | 104 | 2 | FHomogamy |
104 | 0 | 2 | 103 | 2 | FHomogamy |
105 | 0 | 5 | 106 | 6 | FHypogamy |
106 | 1 | 6 | 105 | 5 | FHypogamy |
Here, let's look at person with ID "105", his (because female == 0) education level is 5, his spouse's (person 106's) education level is 6, so it's Female Hypogamy, wive's education > husband's (we assume by default everyone's spouse is of opposite sex).
Now let's look at person with ID "106", since she is person 105's spouse, we also fill the variable "edu_category" with the same "FHypogamy". So essentially, we are looking at every unit of couples.
What I tried:
df2 <- df1 %>%
mutate(edu_category = case_when((SpouseEducation > EducationLevel) | (Female == 1) ~ 'FemaleHypergamy',
(SpouseEducation == EducationLevel) | (Female == 1) ~ 'FemaleHomogamy',
(SpouseEducation < EducationLevel) | (Female == 1) ~ 'FemaleHypogamy',
(SpouseEducation > EducationLevel) | (Female == 0) ~ 'FemaleHypogamy',
(SpouseEducation == EducationLevel) | (Female == 0) ~ 'FemaleHomogamy',
(SpouseEducation < EducationLevel) | (Female == 0) ~ 'FemaleHypergamy'))
However, it's not giving my accurate results - the variable "edu_category" itself is successfully created, but the "FemaleHypergamy", "FemaleHomogamy", and "FemaleHypogamy" are not reflecting accurate situations.
What should I do? Thank you for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种方法可能是使用条件,然后
填充
创建的Na's:One way could be using the conditions and then
fill
the created NA's: