Strsplit in R with a metacharacter

I have a lot of data where the delimiter is a backslash. I process it in R, and it's hard for me to find how to split a string, since the backslash is a metacharacter. For example, the line would look like this:

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

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

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

When I try to use the "fixed" parameter, it does not start because it expects something after the backslash:

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

Unfortunately, I cannot pre-process the data with another program, because the data is generated daily.

+5
source share
3 answers

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

strsplit(tempStr, "\\")

and should be

strsplit(tempStr, "\\\\")

"\\", "\".

+14

, - . , read.table, . , , :

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

, R, :

read.table("file.txt", sep="\\")
+3

Try the following:

strsplit(tempStr, "\"")
-1
source

All Articles