1

I want to create a new variable where if the variable $Who.went.first is contained within the variable $Who.should.go.first then it will return TRUE for the new variable, else it returns FALSE. $Who.should.go.first and $Who.went.first, both have the same set of car names as input, except for some reason all the $Who.should.go.first inputs have the text "(Aspect)" at the end, hence I want the function to check $Who.went.first is contained within $Who.went.first rather than looking for exact matches.

I'm trying to do this using the ifelse function and %in%, as shown below.

Cooperation_2clean$correct.go.first <- ifelse((Cooperation_2clean$Who.went.first %in% Cooperation_2clean$Who.should.go.first), "TRUE", "FALSE")

It will create a new variable, except every case returns FALSE. For example, if $Who.went.first is "AV_0_Blue" and $Who.should.go.first is "AV_0_Blue (Aspect)" then it returns FALSE when it should be true.

Should I be using a different function such as case_when?

EDIT:

Some sample data:

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"))
Darel
  • 13
  • 7
  • 1
    `%in%` is for exact matches, so if you have "AV_0_Blue (Aspect)", the " (Aspect)" part isn't an exact match. Use `%like%` instead to get matches within strings. – Jaccar Jun 28 '19 at 09:43
  • 1
    Can you pleas provide some example data. [Here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) are some hints on how to do it. – eastclintw00d Jun 28 '19 at 09:43
  • I tried replacing %in% with %like% but it returned the error "could not find function "%like%". What package is it in? – Darel Jun 28 '19 at 09:50
  • Just remove the `(Aspect)` part from `Who.should.go.first`. For instance: `Cooperation_2clean$Who.went.first %in% sub("\\s*\\(Aspect\\)","",Cooperation_2clean$Who.should.go.first)`. – nicola Jun 28 '19 at 09:57
  • I'll look into how to use the sub function. I've also added some sample data to my original question. – Darel Jun 28 '19 at 10:01
  • What's your desired output? I think that you need `==` instead of `%in%`. – nicola Jun 28 '19 at 10:29
  • You are correct! The following code worked: `Cooperation_2clean$correct.go.first < ifelse((Cooperation_2clean$Who.went.first == sub("\\s*\\(Aspect\\)","",Cooperation_2clean$Who.should.go.first)), "TRUE", "FALSE")` If what you said as an answer I can mark it as correct. – Darel Jun 28 '19 at 10:42
  • Sorry, I didn't realise `%like%` wasn't in base...it looks like it is in DescTools, but I have never knowingly installed that so must have come with something else. But it's just a wrapper for `grep`. And actually using `grepl` is a logical output so you don't need `ifelse`...so you could do something like: `grepl(Cooperation_2clean$Who.should.go.first, Cooperation_2clean$Who.went.first)` – Jaccar Jun 28 '19 at 10:48
  • I don't think grepl works since I think the pattern has to be a set character string rather than a character variable? Regardless, nicola's suggestion worked. – Darel Jun 28 '19 at 10:54
  • grepl does work. You need to apply it. See answer. – Jon Jun 28 '19 at 13:25

3 Answers3

0

I think grepl is the function you're after. e.g.

biggerstring <- 'LargeItemFindText'
smallstring <-  'geItem'
badstring <- 'notthere'

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

for your example then, you use grepl with an apply function. Working code:

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]))

I think this is a more versatile solution than a specific string replacement because it also captures possible double-spacing, no spacing, bracket use, etc.

Jon
  • 445
  • 3
  • 15
0

Here's my solution

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

lsfischer
  • 344
  • 2
  • 14
0

There is a package called stringr that is made to do this sort of thing.

# 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
Adam Sampson
  • 1,971
  • 1
  • 7
  • 15
  • Notice that I added `stringsAsFactors = FALSE` to your data creation step. Otherwise you have to convert the factors to characters first such as `as.character(Cooperation_2clean$Who.should.go.first)` – Adam Sampson Jun 28 '19 at 14:59