首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ifelse和%中的%在r中创建新变量

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

Stack Overflow用户
提问于 2019-06-28 17:35:20
回答 3查看 221关注 0票数 1

我想创建一个新变量,如果变量$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函数和%中的%来完成此操作,如下所示。

代码语言:javascript
复制
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?

编辑:

以下是一些示例数据:

代码语言:javascript
复制
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"))
EN

回答 3

Stack Overflow用户

发布于 2019-06-28 19:31:29

我认为grepl是你想要的函数。例如:

代码语言:javascript
复制
biggerstring <- 'LargeItemFindText'
smallstring <-  'geItem'
badstring <- 'notthere'

ifelse(grepl(smallstring, biggerstring) > 0, 1, 0)
ifelse(grepl(badstring, biggerstring) > 0, 1, 0)

  • edit

在您的示例中,使用grepl和apply函数。工作代码:

代码语言:javascript
复制
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]))

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

票数 0
EN

Stack Overflow用户

发布于 2019-06-28 21:39:04

这是我的解决方案

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2019-06-28 22:57:39

有一个名为stringr的包就是用来做这类事情的。

代码语言:javascript
复制
# 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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56804445

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档