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

时间:2016-03-14 14:55:53

标签: 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开头的所有内容

1 个答案:

答案 0 :(得分:3)

您可以使用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"