r - grep in ifelse for a vector of values -
i have following dataframe:
data <- data.frame(mobile_number = c( "999/277-135", "075-788-476", "078-388-607", "077/969-557", "076/423-130"))
and want create boolean variable if first 7 characters in string match 1 of values in following vector:
list_values <- c("071", "072", "073", "074", "075", "076", "077", "078", "079")
i can pull values following grep code:
grep(paste(list_values,collapse="|"), data$mobile_number, value=true))
but i'm not sure how put in ifelse statement, , how text on first 7 elements of string.
anyone?
we can use substr
substring of 1st 7 chararacters, apply grepl
on , convert binary (if needed) using as.integer
as.integer(grepl(paste(list_values,collapse="|"), substr(data[[1]], 1, 7)))
Comments
Post a Comment