删除R中文本中的换行符。

问题描述 投票:0回答:1

我想把所有的换行符从我的文本中删除,以便之后将其转化为一个由单个单词分隔的整洁表格。

##load text##
files <- list.files(path = file.path(here(), "data"), pattern = "pdf$")
Indy_movies <- lapply(paste0(file.path(here()),"/data/",files), pdf_text)

结果:

Text

我已经可以看到文本是用 \n分隔的.

##transform to tibble##
Indy_movies <- tibble(movie = c("Indy1", "Indy2", "Indy3", "Indy4"), text = as.character(Indy_movies))

用以下方法转换数据 unnest_tokens() 功能。

Indy_movies %>%
  unnest_tokens(word, text)

结果:

Text

"the "这个词仍然包含了换行符 "n"... 结果是 "nthe".

我已经试过了。

Indy_movies = str_replace_all(Indy_movies, pattern = c("\n" = ""))

这将返回错误: argument is not an atomic vector; coercing. 谢谢!我想删除我的文本中所有的换行符。

r line-breaks
1个回答
0
投票

在没有可重复数据的情况下,这里有一些东西可以让你开始。

数据。

Indy_movies <- "The hovitos are near.\nThe poison etc.\nSome more text"

去掉... \n,使用 gsub 并进行适当的逃逸。

gsub("\\n", " ", Indy_movies)
[1] "The hovitos are near. The poison etc. Some more text"
© www.soinside.com 2019 - 2024. All rights reserved.