Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R strsplit doesn't split on "."?

Tags:

r

strsplit

I am writing an R script and want to define a variable to be used in plot annotations as part of the file name. I thought I would use the strsplit() function. Here is my code and the output:

infile = "ACC_1346.table.txt"

x = strsplit(infile, ".")

class(infile)
[1] "character"

class(x)
[1] "list"

str(x)
List of 1
$ : chr [1:18] "" "" "" "" ...

x[[1]]
[1] "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

I expected the final output to be:

[1] "ACC_1346" "table" "txt"

What is going on here?

like image 580
Slavatron Avatar asked Dec 17 '15 19:12

Slavatron


2 Answers

To avoid regex altogether use fixed = TRUE in the call of strsplit

infile = "ACC_1346.table.txt"
x = strsplit(infile, ".", fixed = TRUE)

x

[[1]]
[1] "ACC_1346" "table" "txt"
like image 94
Andrey Shabalin Avatar answered Nov 15 '22 07:11

Andrey Shabalin


strsplit seeks for a regex to do it's splitting. In regex a "." is a wildcard that pretty much matches anything. To actually match the dot you need to escape using \. Since \ is also an escape character in R, you need to escape it twice as \\.

like image 23
OganM Avatar answered Nov 15 '22 06:11

OganM