Я пытаюсь использовать функцию grep для создания нового столбца на основе сопоставленной строки (без совпадения = 0, совпадения = 1), но не получаю ожидаемых результатов
#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))