【发布时间】:2022-01-22 23:35:31
【问题描述】:
我正在为 kaggle 上的一个项目制作一些 wordcloud,但是这行代码不起作用。我正在尝试从包含文本的列中删除所有撇号。在我的语料库中,“'s”和“'re”是我最常用的两个“词”。虽然数据仍然是数据框的形式,但我一直在使用这行代码df$col <- gsub("\'","", df$col)。
以下是一些示例数据。在我的 kaggle 项目中,文本数据位于数据框的一列中。我错过了什么吗?我也试过str_replace_all 和sub。
编辑:
dput(head(df))
structure(list(X1 = c(0, 1, 2, 3, 4, 5), Character = c("Michael",
"Jim", "Michael", "Jim", "Michael", "Michael"), Line = c("All right Jim. Your quarterlies look very good. How are things at the library?",
"Oh, I told you. I couldn’t close it. So…", "So you’ve come to the master for guidance? Is this what you’re saying, grasshopper?",
"Actually, you called me in here, but yeah.", "All right. Well, let me show you how it’s done.",
"[on the phone] Yes, I’d like to speak to your office manager, please. Yes, hello. This is Michael Scott. I am the Regional Manager of Dunder Mifflin Paper Products. Just wanted to talk to you manager-a-manger. [quick cut scene] All right. Done deal. Thank you very much, sir. You’re a gentleman and a scholar. Oh, I’m sorry. OK. I’m sorry. My mistake. [hangs up] That was a woman I was talking to, so… She had a very low voice. Probably a smoker, so… [Clears throat] So that’s the way it’s done."
), Season = c(1, 1, 1, 1, 1, 1), Episode_Number = c(1, 1, 1,
1, 1, 1)), row.names = c(NA, -6L), class = c("tbl_df", "tbl",
"data.frame"))
编辑 2:
之前我说过df$col <- gsub("\'","", df$col) 在 R 工作室工作。这仅适用于玩具数据。我在 dput 上使用了它,但它没有工作,所以我回到第一方。
【问题讨论】:
-
"[在电话中] 是的,我想和你的办公室经理谈谈。
-
是 I'd、Can't、Don't、Person's 等单词的撇号。
-
这真的不应该改变任何东西,但你不应该在你的正则表达式中转义
'。gsub("'", "", x)甚至gsub("'", "", x, fixed = TRUE)。您确实想要gsub而不是sub因为您想要替换所有单引号,而不仅仅是每个字符串中的第一个。 -
我试过了,但没有运气。这是一个令人沮丧的问题。看起来很简单,我无法解释为什么它不起作用。
-
啊,我明白了。你有“花式报价”,而不是标准报价。查看
substr(df$Line[5], 40, 40),您会发现它是"’"而不是"'"。你可以使用gsub("['’]", "", df$Line)
标签: r string text replace kaggle