简体   繁体   English

如何重新编码三级因素

[英]How to recode a three level factor

My dataset is one which contains information about various children.我的数据集是一个包含各种儿童信息的数据集。 I have a complicated factor question.我有一个复杂的因素问题。 I have two variables: Parent 1 finance and Parent 2 finance (which take one of three values: low, medium, high income).我有两个变量:父母 1 财务和父母 2 财务(采用三个值之一:低、中、高收入)。 I want to make a third variable, "guardian finance" as some of my subjects only have one parent.我想制作第三个变量,“监护人财务”,因为我的一些科目只有一个父母。 How can I recode it so that the highest level of finance is selected, and, if the child is from a one parent household, this is carried over to the new, "Guardian" variable.我如何重新编码以便选择最高级别的财务,并且,如果孩子来自单亲家庭,则将其转移到新的“监护人”变量中。

       p1        n
      <int>    <int>
1      low     100
2      medium  306
3      high    96
        p2            n
       <int>       <int>
1      low         227
2      medium      230
3      high        243

If we want to get the highest 'n' among the the two datsets, do a join between the two columns by the 'p' columnss and then use pmax to return the max between the 'n' columns如果我们想在两个数据集中获得最高的 'n',通过 'p' 列在两列之间进行连接,然后使用pmax返回 '​​n' 列之间的max

library(dplyr)
inner_join(df1, df2, by = c("p1" = "p2") %>%
     mutate(n = pmax(n.x, n.y) %>%
     select(p1, n)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM