删除 R 中以\n 或\U 开头的字符串

标签 r gsub

我有一个像这样的字符串:

x <- c("saw the revenant awesome experience must be seen on a big theatrical screen\ntherevenant","a lil sumn sumn i whipped up \U3e64653c\U3e30613c\U3e64623c\U3e64653c\U3e36623c\U3e61383ctherevenant")

如何使用 gsub() 删除 R 中以\n 或\U 开头的所有内容?

最佳答案

您可以使用 iconv 删除\U 字符,并使用 gsub 处理换行符。

x <- c("saw the revenant awesome experience must be seen on a big theatrical screen\ntherevenant","a lil sumn sumn i whipped up \U3e64653c\U3e30613c\U3e64623c\U3e64653c\U3e36623c\U3e61383ctherevenant")

> iconv(gsub("\\n", " ", x), to="ASCII", sub="")
[1] "saw the revenant awesome experience must be seen on a big theatrical screen therevenant"
[2] "a lil sumn sumn i whipped up therevenant"      

关于删除 R 中以\n 或\U 开头的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35990875/

相关文章:

r - R函数Mclust慢

r - 检查文本中的大写单词并提取它

Ruby gsub 在设置内容时不会更改内容

删除向量的每个元素中第 2 次出现 "-"之后的所有字符

r - 使用 gsub() 替换除某些子字符串之外的所有数字

r - 在 R 中注释维恩图

r - 根据data.table中一列的值改变多列的值

r - ggplot2 - 来自数字/整数数据的离散 X 轴值

r - 使用 gsub() 有选择地将 "-"替换为 "."

r - 从 R 中多个分位数的分位数回归访问 p 值