我想创建一个新变量,如果变量$Who.went.first包含在变量$Who.should.go.first中,那么它将为新变量返回TRUE,否则返回FALSE。$Who.should.go.first和$Who.went.first都有相同的一组汽车名称作为输入,除了某些原因所有$Who.should.go.first输入的末尾都有文本“(方面)”,因此我希望函数检查$Who.went.first是否包含在$Who.went.first中,而不是寻找精确匹配。
我尝试使用ifelse函数和%中的%来完成此操作,如下所示。
Cooperation_2clean$correct.go.first <- ifelse((Cooperation_2clean$Who.went.first %in% Cooperation_2clean$Who.should.go.first), "TRUE", "FALSE")它将创建一个新的变量,除非每个case都返回FALSE。例如,如果$Who.went.first为"AV__Blue“,$Who.should.go.first为"AV__Blue (Aspect)”,则它在应该为true时返回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"))发布于 2019-06-28 19:31:29
我认为grepl是你想要的函数。例如:
biggerstring <- 'LargeItemFindText'
smallstring <- 'geItem'
badstring <- 'notthere'
ifelse(grepl(smallstring, biggerstring) > 0, 1, 0)
ifelse(grepl(badstring, biggerstring) > 0, 1, 0)在您的示例中,使用grepl和apply函数。工作代码:
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"))
Cooperation_2clean$Output <- sapply(1:nrow(Cooperation_2clean), function(x) grepl(Cooperation_2clean$Who.went.first[x],
Cooperation_2clean$Who.should.go.first[x]))我认为这是一个比特定字符串替换更通用的解决方案,因为它还捕获了可能的双倍间距、无间距、括号使用等。
发布于 2019-06-28 21:39:04
这是我的解决方案
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发布于 2019-06-28 22:57:39
有一个名为stringr的包就是用来做这类事情的。
# 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"
),
stringsAsFactors = FALSE
)
library(stringr)
new_var <- str_detect(Cooperation_2clean$Who.should.go.first,Cooperation_2clean$Who.went.first)
# [1] TRUE FALSE TRUE TRUE FALSE
library(stringr)
library(dplyr)
Cooperation_2clean <- Cooperation_2clean %>%
mutate(new_var = str_detect(Who.should.go.first,Who.went.first))
# Who.should.go.first Who.went.first new_var
# 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 FALSEhttps://stackoverflow.com/questions/56804445
复制相似问题