Как отфильтровать строки в каждой строке в R? - PullRequest
0 голосов
/ 15 мая 2019

Я уже много дней пытаюсь решить эту проблему, но не могу получить ожидаемых результатов.

У меня есть информационный фрейм, содержащий разговоры двух человек A & B в каждой строке (это похоже на то, что 1 строка содержит весь разговор, также у меня есть тысячи строк). я хочу отфильтровать строки в каждой строке на основе определенных ключевых слов.

Как я могу это сделать?

Я пробовал ниже строки, но не смог получить точные результаты.

March_Data_fil <- March_Data %>% filter(!str_detect(March_Data, 'Have a good|Thank|day|Ty|thanx|Cheers|How r u|'))

    > head(my_data)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           Transcript
    1 00:00:34 info: You’re now chatting with Bot Virtual Assistant\n00:00:35 Bot: What can I assist with today?\n00:00:35 Bot: \n00:00:45 You: No work\n00:00:48 Bot: Please select your type of work\n00:00:48 Bot: null\n00:00:53 Bot: Please select your location\n00:00:54 Bot: null\n00:01:00 Bot: Thank you, let me connect you with someone to help with this. I'll also pass on the history of our chat.\n00:01:00 Bot: So I can transfer you, please provide me your ID number\n00:18:11 xyz: ill get back to you shortly\n00:18:15 info: Thank you for chatting with us.\n
    2                                           00:05:57 info: You’re now chatting with Bot Virtual Assistant\n00:05:58 Bot: What can I assist with today?\n00:05:58 Bot: \n00:06:17 You: I have no work.\n00:06:19 Bot: Please select your type of work\n00:06:20 Bot: null\n00:06:24 You: I&M\n00:06:25 Bot: Please select your location\n00:06:25 Bot: null\n00:06:28 Bot: Thank you, let me connect you with someone to help with this. I'll also pass on the history of our chat.\n00:06:29 Bot: So I can transfer you, please provide me your ID number\n00:07:49 ***: Thanks\n
    3                                           00:05:57 info: You’re now chatting with Bot Virtual Assistant\n00:05:58 Bot: What can I assist with today?\n00:05:58 Bot: \n00:06:17 You: I have no work.\n00:06:19 Bot: Please select your type of work\n00:06:20 Bot: null\n00:06:24 You: I&M\n00:06:25 Bot: Please select your location\n00:06:25 Bot: null\n00:06:28 Bot: Thank you, let me connect you with someone to help with this. I'll also pass on the history of our chat.\n00:06:29 Bot: So I can transfer you, please provide me your ID number\n00:07:49 ***: Thanks\n
    4 00:00:34 info: You’re now chatting with Bot Virtual Assistant\n00:00:35 Bot: What can I assist with today?\n00:00:35 Bot: \n00:00:45 You: No work\n00:00:48 Bot: Please select your type of work\n00:00:48 Bot: null\n00:00:53 Bot: Please select your location\n00:00:54 Bot: null\n00:01:00 Bot: Thank you, let me connect you with someone to help with this. I'll also pass on the history of our chat.\n00:01:00 Bot: So I can transfer you, please provide me your ID number\n00:18:11 xyz: ill get back to you shortly\n00:18:15 info: Thank you for chatting with us.\n
    5                                           00:05:57 info: You’re now chatting with Bot Virtual Assistant\n00:05:58 Bot: What can I assist with today?\n00:05:58 Bot: \n00:06:17 You: I have no work.\n00:06:19 Bot: Please select your type of work\n00:06:20 Bot: null\n00:06:24 You: I&M\n00:06:25 Bot: Please select your location\n00:06:25 Bot: null\n00:06:28 Bot: Thank you, let me connect you with someone to help with this. I'll also pass on the history of our chat.\n00:06:29 Bot: So I can transfer you, please provide me your ID number\n00:07:49 ***: Thanks\n
       ID
    1 231
    2 243
    3 222
    4 123
    5 234
> str(my_data)
'data.frame':   5 obs. of  2 variables:
 $ Transcript: chr  "00:00:34 info: You’re now chatting with Bot Virtual Assistant\n00:00:35 Bot: What can I assist with today?\n00:"| __truncated__ "00:05:57 info: You’re now chatting with Bot Virtual Assistant\n00:05:58 Bot: What can I assist with today?\n00:"| __truncated__ "00:05:57 info: You’re now chatting with Bot Virtual Assistant\n00:05:58 Bot: What can I assist with today?\n00:"| __truncated__ "00:00:34 info: You’re now chatting with Bot Virtual Assistant\n00:00:35 Bot: What can I assist with today?\n00:"| __truncated__ ...
 $ ID        : int  231 243 222 123 234

Может кто-нибудь, пожалуйста, помогите мне, я застрял на этом с неделю: (

Спасибо, Насир

Ответы [ 2 ]

1 голос
/ 15 мая 2019

Один из вариантов - разделить строки на символы новой строки, удалить соответствующие части и повторно объединить результаты в строки (при условии, что ваши данные находятся в символьном векторе x):

remove_pattern = 'Have a good|Thank|day|Ty|thanx|Cheers|How r u'
res = lapply(strsplit(x, "\n", fixed = TRUE), function(x) {
  paste(grep(remove_pattern, x, value = TRUE, invert = TRUE), collapse= "\n")
})

invisible(lapply(res, cat))
# 00:00:34 Botmessage: You’re now chatting with Botmessage Virtual Assistant
# 00:00:35 Botmessage: 
# 00:00:45 You: No work
# 00:00:48 Botmessage: Please select your type of work
# 00:00:48 Botmessage: null
# 00:00:51 You: I&M
# 00:01:24 Botmessage: 
# 00:01:25 Botmessage: Please wait while your chat is transferred to the appropriate group.00:05:18 Botmessage: You’re now chatting with Botmessage Virtual Assistant
# 00:05:20 Botmessage: 
# 00:08:07 You: No work
# 00:08:08 Botmessage: Please select your type of work
# 00:08:08 Botmessage: null
# 00:08:12 You: I&M
# 00:08:14 Botmessage: Please select your location
# 00:08:21 Botmessage: So I can transfer you, please provide me your ID number
# 00:08:33 Botmessage: 
# 00:08:33 Botmessage: Please wait while your chat is transferred to the appropriate group.
0 голосов
/ 15 мая 2019

Обновление: этот ответ предполагал другой желаемый вывод.

Попробуйте передать переменную, содержащую строки, вместо всего March_Data кадра данных в str_detect.Кроме того, я не знаю str_detect, но это будет работать, если предположить, что March_Data - это кадр данных

March_Data_fil <- March_Data %>% dplyr::filter(
  !grepl('Have a good|Thank|day|Ty|thanx|Cheers|How r u|', variable_containing_strings))

Воспроизводимый пример:

dplyr::filter(iris, !grepl('setosa|virginica', Species))
...