Curious
Curious

Reputation: 549

Create a new column based on matched string in another column using grep function

I'm trying to use grep function to create a new column based on matched string (no match = 0, match =1) but not getting the expected results

#my data
data<- data.frame(col1 = c("name","no_match","123","0.19","rand?m","also_no_match"))

#string to match
match_txt <- "[0-9]\\.[0-9][0-9]|name|name2|wh??|[0-9]|\\?"  #used "|" to match multiple strings

#create the new column using for loop
for (i in 1:nrow(data))
  {
data$col2[i] <- grep(match_txt , data$col1[i])
}

# I get the error below:
# Error in data$col2[i] <- grep(match_txt, data$col1[i],  ignore.case = TRUE) : replacement has length zero

#this is expected correct results:
expected_data <- data.frame(col1 = c("name","no_match","123","0.19","rand?m","also_no_match"),
                            col2 = c(1,0,1,1,1,0))

Upvotes: 1

Views: 235

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388817

grep/grepl are vectorised. We can directly use them on the column with the pattern, just wrap it in as.integer to convert logical values TRUE/FALSE to 1/0 respectively.

data$col2 <- as.integer(grepl("[0-9]\\.[0-9][0-9]|name|name2|wh??|[0-9]|\\?", data$col1))
data
#           col1 col2
#1          name    1
#2      no_match    0
#3           123    1
#4          0.19    1
#5        rand?m    1
#6 also_no_match    0

Upvotes: 3

Related Questions