首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何过滤掉R中每一行中的行?

如何过滤掉R中每一行中的行?
EN

Stack Overflow用户
提问于 2019-05-15 19:13:26
回答 2查看 121关注 0票数 0

我已经在这个问题上尝试了很多天,但没有得到预期的结果。

我有一个数据帧,每行包含两个人A和B的对话(就像1行包含整个对话,同样我有数千行)。我想根据某些关键字过滤掉每一行中的行。

我该怎么做呢?

我试过以下几行,但没有得到确切的结果。

代码语言:javascript
运行
复制
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

EN

Stack Overflow用户

发布于 2019-05-15 19:21:59

更新:这个答案假设了一个不同的期望输出。

尝试向str_detect传递包含字符串的变量,而不是整个March_Data数据帧。此外,我不知道str_detect,但假设March_Data是一个数据帧,这将会起作用

代码语言:javascript
运行
复制
March_Data_fil <- March_Data %>% dplyr::filter(
  !grepl('Have a good|Thank|day|Ty|thanx|Cheers|How r u|', variable_containing_strings))

可重现的例子:

代码语言:javascript
运行
复制
dplyr::filter(iris, !grepl('setosa|virginica', Species))
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56148018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档