strsplit in R with a metacharacter
Asked Answered
C

3

5

I have a large amount of data where the delimiter is a backslash. I'm processing it in R and I'm having a hard time finding how to split the string since the backslash is a metacharacter. For example, a string would look like this:

1128\0019\XA5\E2R\366\00=15

and I want to split it along the \ character, but when I run the strsplit command:

strsplit(tempStr, "\\")
Error in strsplit(tempStr, "\\") : 
  invalid regular expression '\', reason 'Trailing backslash'

When I try to used the "fixed" option, it does not run because it is expecting something after the backslash:

strsplit(tempStr, "\", fixed = TRUE)

Unfortunately, I can't preprocess the data with another program because the data is generated daily.

Crematory answered 1/6, 2012 at 4:21 Comment(0)
C
14

Your line of code is (though you don't say it explicitly):

strsplit(tempStr, "\\")

and should be

strsplit(tempStr, "\\\\")

This will be read as "\\", which the regular expression will understand as a "\".

Columbarium answered 1/6, 2012 at 4:36 Comment(4)
unfortunately this is not working... I just get the same string back. Any other ideas? Thanks.Crematory
Could you please provide a self-contained example (that is, get it into R code so we can run it?)Columbarium
Aye, same problem... when I do \\\\, it removes any occurrences of double slash, but \\ gets the above error, and \\\ tries to escape the following quote mark...Fellows
@JoshuaEricTurcotte could you provide a self-contained example?Columbarium
O
3

I suspect that your data is in a file of some sort. If this is the case, then use read.table but specify the delimiter explicitly. For example, suppose your file was:

a\b\c\d
e\f\g\h

then to read this into R, use the command:

read.table("file.txt", sep="\\")
Oddment answered 1/6, 2012 at 8:52 Comment(0)
W
-1

Try this:

strsplit(tempStr, "\"")
Webby answered 10/4, 2017 at 7:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.