[R] detect escape character
David Winsemius
dwinsemius at comcast.net
Wed Jun 4 02:44:33 CEST 2014
On Jun 3, 2014, at 11:03 AM, Adrian Dușa wrote:
> Dear All,
>
> I should be knowing this, but not get it right...
I'm a little surprised to see you ask this, too, but we each have lacunae in our R knowledge, so I hope this helps:
> s <- scan(what="") # Assuming you were asking about items coming in from ASCII data files
1: Man\Woman
2:
Read 1 item
> s
[1] "Man\\Woman"
> gsub("\\", "/", s)
Error in gsub("\\", "/", s) :
invalid regular expression '\', reason 'Trailing backslash'
> gsub("\\\\", "/", s)
[1] "Man/Woman"
_Each_ of the backslashes in what you might have thought would succeed ("\\") also needed their own backslash. The first doubled backslash becomes a real "escape"-character in the pattern and the second doubled backslash becomes an ordinary ASCII backslash character.
--
David.
> For a string like this:
>
> "Man\Woman"
>
> I would like to detect the escape character "\" and replace it with "/".
>
> Tried various ways using gsub(), but don't get it right yet. Any suggestion
> would be highly welcomed...
>
> Thank you,
> Adrian
>
> --
> Adrian Dusa
> University of Bucharest
> Romanian Social Data Archive
> 1, Schitu Magureanu Bd.
> 050025 Bucharest sector 5
> Romania
> Tel.:+40 21 3126618 \
> +40 21 3120210 / int.101
> Fax: +40 21 3158391
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
David Winsemius
Alameda, CA, USA
More information about the R-help
mailing list