r - 使用 ifelse 和 %in% 在 r 中创建一个新变量
问题描述
我想创建一个新变量,如果变量 $Who.went.first 包含在变量 $Who.should.go.first 中,那么它将为新变量返回 TRUE,否则返回 FALSE。$Who.should.go.first 和 $Who.went.first 都具有相同的汽车名称集作为输入,但出于某种原因,所有 $Who.should.go.first 输入都具有文本“(Aspect)”最后,因此我希望该函数检查 $Who.went.first 是否包含在 $Who.went.first 中,而不是寻找精确匹配。
我正在尝试使用 ifelse 函数和 %in% 来执行此操作,如下所示。
Cooperation_2clean$correct.go.first <- ifelse((Cooperation_2clean$Who.went.first %in% Cooperation_2clean$Who.should.go.first), "TRUE", "FALSE")
它将创建一个新变量,除非每种情况都返回 FALSE。例如,如果 $Who.went.first 是“AV_0_Blue”,而 $Who.should.go.first 是“AV_0_Blue (Aspect)”,那么它应该为真时返回 FALSE。
我是否应该使用不同的函数,例如 case_when?
编辑:
一些样本数据:
Cooperation_2clean <- data.frame("Who.should.go.first" = c("AV_0_Blue (Aspect)", "Human_2_BlueCW (Aspect)", "AV_0_Blue (Aspect)", "AV_2_Green (Aspect)", "AV_3_Orange (Aspect)"), "Who.went.first" = c("AV_0_Blue", "AV_3_Orange", "AV_0_Blue", "AV_2_Green", "AV_2_Green"))
解决方案
这是我的解决方案
library("tidyverse")
# Your sample dataframe
Cooperation_2clean <-
data.frame(
"Who.should.go.first" = c(
"AV_0_Blue (Aspect)",
"Human_2_BlueCW (Aspect)",
"AV_0_Blue (Aspect)",
"AV_2_Green (Aspect)",
"AV_3_Orange (Aspect)"
),
"Who.went.first" = c(
"AV_0_Blue",
"AV_3_Orange",
"AV_0_Blue",
"AV_2_Green",
"AV_2_Green"
)
)
# Create a new column named "new_var" where we check rowise
# if the string in Who.went.first is contained in Who.should.go.first
Cooperation_2clean %>%
rowwise() %>%
mutate(new_var = grepl(Who.went.first, Who.should.go.first))
# Who.should.go.first Who.went.first new_var
# <fct> <fct> <lgl>
# 1 AV_0_Blue (Aspect) AV_0_Blue TRUE
# 2 Human_2_BlueCW (Aspect) AV_3_Orange FALSE
# 3 AV_0_Blue (Aspect) AV_0_Blue TRUE
# 4 AV_2_Green (Aspect) AV_2_Green TRUE
# 5 AV_3_Orange (Aspect) AV_2_Green FALSE