简体   繁体   English

如何根据 R 中的两个分类值创建新变量?

[英]How do I create a new variable based on two categorical values in R?

I have a marital status variable, with possible values "Married" "Divorced" "Widowed" "Separated" "Never Married" "Part of an unmarried couple".我有一个婚姻状况变量,可能的值是“已婚”“离婚”“丧偶”“分居”“未婚”“未婚夫妇的一部分”。

I wish to create a new variable called single , where if marital is "Married" OR "Part of an unmarried couple", it is classified as "Not Single", else "Single".我希望创建一个名为single的新变量,如果marital是“已婚”或“未婚夫妇的一部分”,则将其归类为“Not Single”,否则归类为“Single”。

Was thinking something like below, but with some kind of OR operator ie "Married" OR "Part of an unmarried couple".正在考虑类似下面的内容,但使用某种 OR 运算符,即“已婚”或“未婚夫妇的一部分”。

dataset <- dataset %>%
mutate(single = ifelse(marital == “Married”, "Not Single", " Single"))

I am very new to R and hope someone can help.我对 R 很陌生,希望有人能提供帮助。 Thanks for your time!谢谢你的时间!

You already found a good solution, but since you specifically asked about the or operator, in R it's |您已经找到了一个很好的解决方案,但是由于您特别询问了 or 运算符,因此在 R 它是| . .

dataset <- dataset %>%

mutate(single = ifelse( marital == "Married" | marital == "Part of an unmarried couple",
                        "Not single", 
                        "single")

Note that its |请注意,它的| , not || , 不是|| :

& and && indicate logical AND and | & 和 && 表示逻辑 AND 和 | and ||和 || indicate logical OR.表示逻辑或。 The shorter form performs elementwise comparisons in much the same way as arithmetic operators.较短的形式执行元素比较的方式与算术运算符大致相同。 The longer form evaluates left to right examining only the first element of each vector.较长的形式从左到右评估,仅检查每个向量的第一个元素。 Evaluation proceeds only until the result is determined.仅在确定结果之前进行评估。 The longer form is appropriate for programming control-flow and typically preferred in if clauses.较长的形式适用于编程控制流,通常在 if 子句中首选。

(Copied from https://stat.ethz.ch/R-manual/R-devel/library/base/html/Logic.html ) (复制自https://stat.ethz.ch/R-manual/R-devel/library/base/html/Logic.html

暂无
暂无

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

相关问题 如何从 R 中的两个现有分类变量创建新变量 - How to create new variable from two existing categorical variables in R 如何根据行值合并 R 中的数据并用它创建新变量? - How do I merge data in R based on row values and create new variable with it? 如何从一个变量中创建两个新变量,并在 R 中为其附加虚拟值? - How do I create two new variables out of one variable, and attach dummy values to it in R? 根据R中的时间戳设置新的分类变量值 - Setting New categorical variable values based on Timestamp in R R:基于连续变量从分类变量创建新的分类变量 - R: create a new categorical variable from a categorical variable based on a continuous variable 基于 data.table R 中的两列创建一个分类变量 - Create a categorical variable based on two columns in data.table R 如何使用 R 根据长格式数据中列中第一个零的位置创建新的分类变量? - How to create a new categorical variable based on the location of first zero in a column in a long format data using R? 如何根据其他两列的值在 R 中创建新变量? - How to create a new variable in R based on values from two other columns? 如何根据 R 中的特定分类列值创建两个金额列 - How to Create Two Amount Columns Based on Specific Categorical Column Values in R 如何基于分类变量在 R Plotly 中创建叶绿体图? - How to create a chloropleth map in R Plotly based on a Categorical variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM