使用ifelse和%in%在r中创建新变量

时间:2019-06-28 09:35:19

标签: r if-statement

我想创建一个新变量,如果变量$ 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)”,则当它为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"))

3 个答案:

答案 0 :(得分:0)

我认为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]))

我认为这是比特定字符串替换更通用的解决方案,因为它还捕获了可能的双倍空格,无间距,使用括号等。

答案 1 :(得分:0)

这是我的解决方法

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

答案 2 :(得分:0)

有一个名为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   FALSE