替换 R 中的单个反斜杠 [英] Replace single backslash in R

查看:60
本文介绍了替换 R 中的单个反斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a string that looks like:

str<-"a\f\r"

我正在尝试删除反斜杠,但没有任何效果:

I'm trying to remove the backslashes but nothing works:

gsub("\","",str, fixed=TRUE)
gsub("\\","",str)
gsub("(\)","",str)
gsub("([\])","",str)

...基本上你能想象到的所有变化.我什至尝试过 string_replace_all 函数.有什么帮助吗??

...basically all the variations you can imagine. I have even tried the string_replace_all function. ANY HELP??

我使用的是 R 版本 3.1.1;Mac OSX 10.7;我的字符串向量中单个字符串的 dput 给出:

I'm using R version 3.1.1; Mac OSX 10.7; the dput for a single string in my vector of strings gives:

dput(line)
"ud83d\ude21\ud83d\udd2b"

我使用导入的文件readLines 来自标准.txt 文件.该文件的内容类似于:<代码>今天下午预约了一名工程师 \ud83d\udc4d 现在希望全部排序\ud83d\ude0a 我要在 utf8towcs 中发疯 ud83d\ude21\ud83d\udd2b ...

I imported the file using readLines from a standard .txt file. The content of the file looks something like: got an engineer booked for this afternoon \ud83d\udc4d all now hopefully sorted\ud83d\ude0a I m going to go insane ud83d\ude21\ud83d\udd2b in utf8towcs …

谢谢.

推荐答案

由于没有任何直接的方法来处理单个反斜杠,这里是 David Arenburg 在评论部分提供的最接近该问题的解决方案

Since there isn't any direct ways to dealing with single backslashes, here's the closest solution to the problem as provided by David Arenburg in the comments section

gsub("[^A-Za-z0-9]", "", str) #remove all besides the alphabets & numbers

这篇关于替换 R 中的单个反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆