简体   繁体   English

基于与 R 中其他列的组合,使用 ifelse 语句创建新列

[英]Create new column with ifelse statement based on combination with other columns in R

I have a dataset with the two columns urban_rural and religious.我有一个包含urban_rural 和宗教两列的数据集。 I want to create a new column based on the two specific interactions between these two columns.我想根据这两列之间的两个特定交互创建一个新列。 That is, urban_not_religious and rural_religious.即,urban_not_religious 和urban_religious。

After some research on this website, I managed to create a new column based on the combination of the two columns with an ifelse statement, but only for those respondents that satisfy both conditions.在此网站上进行了一些研究后,我设法根据两列的组合和 ifelse 语句创建了一个新列,但仅限于满足这两个条件的受访者。 For all others, I tried to write another ifelse statement, but then it returns only NAs.对于所有其他人,我尝试编写另一个 ifelse 语句,但它只返回 NA。 I could give all respondents with NAs the value of "No", but I want to keep NAs seperate to those which have valid values, but do not satisfy the conditions.我可以给所有具有 NA 的受访者的值“否”,但我希望将 NA 与具有有效值但不满足条件的那些分开。 So those who live in urban areas and are not religious with "Yes", all other respondents with "No", and NAs.因此,居住在城市地区且不信教的人选择“是”,所有其他受访者都选择“否”,以及 NA。

Here is a sample of my dataset and the code I used:这是我的数据集示例和我使用的代码:

structure(list(urban_rural = structure(c(1L, 1L, 2L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 
1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Urban", 
"Rural", "Refugee camp"), class = "factor"), religious = structure(c(2L, 
1L, 2L, 2L, 3L, 2L, 2L, 3L, 1L, 3L, 3L, 1L, 3L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 2L, 3L, 2L, 2L, 2L, 
3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 1L, 2L, 2L, 2L, 2L, 
1L), .Label = c("Religious", "Somewhat religious", "Not religious"
), class = "factor")), row.names = c(NA, 50L), class = "data.frame")

dataset$urban_not_reg <- ""

dataset <- dataset %>%
  mutate(urban_not_reg=ifelse((urban_rural=="Urban")&(religious=="Not religious"),"Yes",NA)) %>%
  mutate(urban_not_reg=ifelse((urban_rural=="Rural")&(urban_rural=="Refugee camp")&(religious=="Religious")&(religious=="Somewhat religious"),"No",NA))

You can assign "No" to anything which is not "Urban" after assigning "Yes" .分配"Yes"后,您可以将"No"分配给不是"Urban"任何内容。

library(dplyr)

dataset %>%
  mutate(urban_not_reg = case_when(urban_rural=="Urban" & religious == "Not religious" ~ "Yes", 
                                   urban_rural != "Urban" ~ "No", 
                                   TRUE ~ NA_character_))

Does this work:这是否有效:

> dataset %>% mutate(urban_not_reg = case_when(urban_rural == 'Urban' & religious == 'Not religious' ~ 'Yes',
+                                             (urban_rural == "Rural" & religious=="Religious") | 
+                                             (urban_rural == "Refugee camp" & religious=="Somewhat religious") | 
+                                             (urban_rural == "Rural" & religious=="Somewhat religious") |
+                                             (urban_rural == "Refugee camp" & religious=="Religious")  ~ 'No',
+                                              TRUE ~ NA_character_))

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

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