I am trying to check for backslashes in data, then remove them when I find them, but am having a difficult time figuring out the best way to do it. I know the backslash is the escape character in R, and I should be able to use 'gsub' to accomplish this, but I all I seem to be getting are errors. For example: If entry is: "Hello\World" I want: "HelloWorld" There are several entries with backslashes, and I need to find them and delete them. All help is welcome, thank you. ___________________________________ Drew Conway Ph.D. Student Department of Politics, New York University agc282@nyu.edu http://homepages.nyu.edu/~agc282 [[alternative HTML version deleted]]
Below works but it has two backslashes in the word. maybe someone can explain why the 4 and? 2 works but 2 1 doesn't ? thanks. gsub("\\\\","","Hello\\World",perl=TRUE) On Apr 3, 2009, Andrew Conway <agc282 at nyu.edu> wrote: I am trying to check for backslashes in data, then remove them when I find them, but am having a difficult time figuring out the best way to do it. I know the backslash is the escape character in R, and I should be able to use 'gsub' to accomplish this, but I all I seem to be getting are errors. For example: If entry is: "Hello\World" I want: "HelloWorld" There are several entries with backslashes, and I need to find them and delete them. All help is welcome, thank you. ___________________________________ Drew Conway Ph.D. Student Department of Politics, New York University [1]agc282 at nyu.edu [2]http://homepages.nyu.edu/~agc282 [[alternative HTML version deleted]] ______________________________________________ [3]R-help at r-project.org mailing list [4]https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide [5]http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. References 1. mailto:agc282 at nyu.edu 2. http://homepages.nyu.edu/~agc282 3. mailto:R-help at r-project.org 4. https://stat.ethz.ch/mailman/listinfo/r-help 5. http://www.R-project.org/posting-guide.html
On 4/3/2009 2:33 PM, Andrew Conway wrote:> I am trying to check for backslashes in data, then remove them when I > find them, but am having a difficult time figuring out the best way to > do it. I know the backslash is the escape character in R, and I > should be able to use 'gsub' to accomplish this, but I all I seem to > be getting are errors. For example: > > If entry is: > "Hello\World" > > I want: > "HelloWorld" > > There are several entries with backslashes, and I need to find them > and delete them. All help is welcome, thank you.This gets a little confusing because print() automatically doubles backslashes. To see what's really going on, you need to use cat. Then the following can be seen to work: input <- "\"Hello\\World\"" cat(input, "\n") output <- gsub("\\\\", "", input) cat(output, "\n") Duncan Murdoch> > ___________________________________ > Drew Conway > Ph.D. Student > Department of Politics, New York University > agc282 at nyu.edu > http://homepages.nyu.edu/~agc282 > > > [[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.
Seemingly Similar Threads
- edit() doubles backslashes when keep.source=TRUE
- Upgrading from 1.4.21.2 to 1.6.0.5 breaks sql queries with backslashes?
- gsub: replacing double backslashes with single backslash
- should Sys.glob() cope with a UNC windows path beginning with backslashes?
- Gnome-terminal's backslashes look like Ws with a horizontal line through -- how to get a backslash?