Я R newb ie. Мне нужно разбить свободный текст (отзывы клиентов) на фиксированное количество категорий. Я пытаюсь запустить небольшой код для проверки логики c.
a<-c("a","b","c","d","e") # Category a - if the free text contains any of "a","b","c","d" or "e"
b<-c("f","g","h","i","j") # Category b - if the free text contains any of "f","g","h","i" or "j"
check<-c("a","g","d","j") # Free text to be categorized. "a" should be categorized as a; "g" as b; "d" as a and
# "j" as b
count<-length(check)
output<-vector(mode="list",length = count) # Empty categorized list - targeted output is (a,b,a,b)
for (i in 1:count) {
output[i]<-ifelse(grepl(a,check[i]),"a",ifelse(grepl(b,check[i]),"b","other"))
}
Я получаю следующие предупреждения:
1. In grepl(a, check[i]) :
argument 'pattern' has length > 1 and only the first element will be used
2. In grepl(a, check[i]) :
argument 'pattern' has length > 1 and only the first element will be used
3. In grepl(b, check[i]) :
argument 'pattern' has length > 1 and only the first element will be used
4. In grepl(a, check[i]) :
argument 'pattern' has length > 1 and only the first element will be used
5. In grepl(b, check[i]) :
argument 'pattern' has length > 1 and only the first element will be used
6. In grepl(a, check[i]) :
argument 'pattern' has length > 1 and only the first element will be used
7. In grepl(b, check[i]) :
argument 'pattern' has length > 1 and only the first element will be used
Вывод выходит как (a, other, other , прочее)
Либо grepl не подходит для использования, либо, возможно, есть способ использовать векторный шаблон. Запрос вашей помощи и руководства.