Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Split Character String at Underscore Using scan() Function in R – strsplit() vs. scan() Comparison

I’ve noticed that the functions strsplit() and scan() handle underscores differently, and I’m wondering why this is the case.

Please consider the following example code:

x1 <- "string split"

strsplit(x1, " ")[[1]]
# [1] "string" "split" 

scan(text = x1, what = " ")
# [1] "string" "split" 

The outputs of strsplit and scan are the same when using " " as a separator.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

However, when I use "_" as a separator, the output is different:

x2 <- "string_split"

strsplit(x2, "_")[[1]]
# [1] "string" "split" 

scan(text = x2, what = "_")
# [1] "string_split"

Why is the output of strsplit and scan different when using underscores as a separator?

>Solution :

the argument what is not the separator but rather the data type. IN your case, you have character:

scan(text=x2, what = character(), sep='_', quiet = TRUE)
[1] "string" "split" 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading