2

我正在尝试使用 R 来分析下载的一些 Facebook 消息。一些消息将撇号替换为“â” - 我正在尝试使用 str_replace_all() 替换它。

举个例子,下面的data.table

names <- c("Me", "Me", "You", "You", "Me", "You")
content <- c("Iâ<U+0080><U+0099>ve got my party on the 5th", "Hello", "Bears", "Four times four", "what do you want to eat?", "get some music")
date <- c("1/1/2001", "2/1/2001", "3/1/2001", "4/1/2001", "5/1/2001", "6/1/2001")
fbmessagesexample <- data.table(names, date, content)

然后我尝试使用 str_replace_all

fbmessagesexample[, content := str_replace_all(content, pattern = fixed("â<U\\+0080><U\\+0099>"), replacement=fixed("'"))]

内容中的第一行没有被替换。有什么我做错了吗?

4

1 回答 1

1

请为 传递一个向量pattern

以下代码片段导致控制台输出,如下所示。

library(data.table)
library(tidyverse)

names <- c("Me", "Me", "You", "You", "Me", "You")
content <- c("Iâ<U+0080><U+0099>ve got my party on the 5th", "Hello", "Bears", "Four times four", "what do you want to eat?", "get some music")
date <- c("1/1/2001", "2/1/2001", "3/1/2001", "4/1/2001", "5/1/2001", "6/1/2001")
fbmessagesexample <- data.table(names, date, content)

pattern <- c("â<U\\+0080><U\\+0099>")

fbmessagesexample[, content := str_replace_all(content, pattern, replacement=fixed("'"))]

安慰:

> fbmessagesexample
   names     date                      content
1:    Me 1/1/2001 I've got my party on the 5th
2:    Me 2/1/2001                        Hello
3:   You 3/1/2001                        Bears
4:   You 4/1/2001              Four times four
5:    Me 5/1/2001     what do you want to eat?
6:   You 6/1/2001               get some music
于 2019-12-24T11:55:50.537 回答