strsplit in R with a metacharacter

非 Y 不嫁゛ 提交于 2019-12-12 10:53:57

问题


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.


回答1:


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 "\".




回答2:


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="\\")



回答3:


Try this:

strsplit(tempStr, "\"")


来源:https://stackoverflow.com/questions/10844364/strsplit-in-r-with-a-metacharacter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!