我已经在这个问题上尝试了很多天,但没有得到预期的结果。
我有一个数据帧,每行包含两个人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有没有人能帮帮我,我已经被困在这一周了:
谢谢,Naseer
发布于 2019-05-15 19:21:59
更新:这个答案假设了一个不同的期望输出。
尝试向str_detect传递包含字符串的变量,而不是整个March_Data数据帧。此外,我不知道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))https://stackoverflow.com/questions/56148018
复制相似问题