【问题标题】:How to filter out lines within each row in R?如何过滤掉R中每一行中的行?
【发布时间】:2019-05-18 19:57:27
【问题描述】:

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

我有一个数据框,其中每行包含两个人 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

有人可以帮帮我吗,我已经坚持了一周:(

谢谢, 纳赛尔

【问题讨论】:

  • 你得到了什么结果?能否提供一些示例数据?
  • 请阅读How to Create a Minimal, Reproducible Example 并更新您的问题。
  • 问题已相应更新。
  • 你能用head(March_Data)更新一下,这样我们就可以看到完整的结构了吗?
  • 嗨,彼得,用 head 更新了问题。

标签: r filter text-mining preprocessor


【解决方案1】:

一种选择是用换行符分割字符串,删除匹配部分并将结果重新组合成字符串(假设您的数据在字符向量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.

【讨论】:

  • 嗨 Docendo,当应用于数据框列时,代码会抛出空列表。我已经用 head() & str() 更新了这个问题。请看一看。
【解决方案2】:

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

尝试将包含字符串的变量而不是整个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))

【讨论】:

  • 嗨皮特,使用您的代码,它完全删除了所有行。我想删除每一行中的行。我希望你明白我的意思。每行包含大约 30-50 行对话。
猜你喜欢
  • 2022-12-21
  • 1970-01-01
  • 2022-01-03
  • 1970-01-01
  • 2013-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多