If Else Statements to Check If a String Contains a Substring in R

If else statements to check if a string contains a substring in R

Assume you have a vector of characters, you can use stringr::str_extract for this purpose:

s <- c('A, C, D', 'P, O, E', 'W, E, W', 'S, B, W')
s
# [1] "A, C, D" "P, O, E" "W, E, W" "S, B, W"
stringr::str_extract(s, 'A|B')
# [1] "A" NA NA "B"

If a word match is preferred, use word boundaries \\b:

stringr::str_extract(s, '\\b(A|B)\\b')
# [1] "A" NA NA "B"

If substring is defined by ", ", you can use this regex (?<=^|, )(A|B)(?=,|$):

# use the test case from G.Grothendieck
stringr::str_extract(c("A.A, C", "D, B"), '(?<=^|, )(A|B)(?=,|$)')
# [1] NA "B"

Test if characters are in a string

Use the grepl function

grepl( needle, haystack, fixed = TRUE)

like so:

grepl(value, chars, fixed = TRUE)
# TRUE

Use ?grepl to find out more.

R - check for substring within string, if true then add value to a different variable

You can try

 ws$distance[grep('80m', ws$stim)] <- 80

data

set.seed(24)
ws <- data.frame(distance=sample(40:90, 20, replace=TRUE),
stim=sample(paste0(c(20,40,60,80),'m'), 20,
replace=TRUE), stringsAsFactors=FALSE)

If string contains x do y in R

You could use grepl:

ifelse(grepl("1", Fixations$ID), "1", 
ifelse(grepl("2", Fixations$ID), "2", NA))

The last parameter defines the value to assign when neither "1" or "2" occurs.

Create column based on presence of string pattern and ifelse

To check if a string contains a certain substring, you can't use == because it performs an exact matching (i.e. returns true only if the string is exactly "non").

You could use for example grepl function (belonging to grep family of functions) that performs a pattern matching:

df$loc01 <- ifelse(grepl("non",df$loc_01),'outside','inside')

Result :

> df
loc_01 loc01_land loc01
1 apis 165730500 inside
2 indu 62101800 inside
3 isro 540687600 inside
4 miss 161140500 inside
5 non_apis 1694590200 outside
6 non_indu 1459707300 outside
7 non_isro 1025051400 outside
8 non_miss 1419866100 outside
9 non_piro 2037064500 outside
10 non_sacn 2204629200 outside
11 non_slbe 1918840500 outside
12 non_voya 886299300 outside
13 piro 264726000 inside
14 sacn 321003900 inside
15 slbe 241292700 inside
16 voya 530532000 inside

If Column Contains String then put value for that row

Does this work:

library(dplyr)
library(stringr)
df %>% mutate(B = case_when(str_detect(A, 'Sales|Marketing') ~ 'Yes', TRUE ~ 'No'))
A B
1 Manager, Sales Yes
2 Manager No
3 Manager, Marketing Yes
4 Manager, Marketing, Sales Yes

In R loop through rows if condition is met and string value is contained in a character vector set new column value to the character vector element

You can try -

#Initialise record column to NA
df$record <- NA
#get the row numbers where style_name is 'Title'
inds <- which(df$style_name == 'Title')
#For each index find the corresponding filenames which matches.
for(i in inds) {
val <- grep(df$text[i], filenames, value = TRUE)
if(length(val)) df$record[i] <- val[1]
}
df

# style_name text record
#1 Title ABC Co C:/Temp/data/D21 248694 Company Data - ABC Co - August 2021.DOCX
#2 List Bullet blah blah <NA>
#3 List Bullet blah blah <NA>
#4 Title XYZ Limited C:/Temp/data/D21 248706 Company Data – XYZ Limited – September 2021.DOCX
#5 List Bullet blah blah <NA>

If Column Contains String then enter value for that row

We can use grepl to return a logical index by matching the 'D' in the 'A' column, and then with ifelse, change the logical vector to 'yes' and 'no'

df$C <- ifelse(grepl("D", df$A), "yes", "no")

Add a word to a string based on a conditional statement

Let me write up my ideas since the OP requested it. One way is to use gsub(). You look for the pattern, "one" and replace it by pasting "one", which is represented as \\1, and newword. The other way is to use which(). You look for index numbers for the element, "one". For those index number positions, you want to add paste("one", newword, sep = " ").

gsub(x = words, pattern = "(one)", replacement = paste("\\1", newword, sep = " "))

words[which(words == "one")] <- paste("one", newword, sep = " ")


Related Topics



Leave a reply



Submit