Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strsplit not consistently working, character between letters isn't a space?

The problem is very simple, but I'm having no luck fixing it. strsplit() is a fairly simple function, and I am surprised I am struggling as much as I am:

# temp is the problem string. temp is copy / pasted from my R code.
# i am hoping the third character, the space, which i think is the error, remains the error 
temp = "GS PG"

# temp2 is created in stackoverflow, using an actual space
temp2 = "GS PG"

unlist(strsplit(temp, split = " "))
[1] "GS PG"
unlist(strsplit(temp2, split = " "))
[1] "GS" "PG"

.
even if it doesn't work here with me trying to reproduce the example, this is the issue i am running into. with temp, the code isn't splitting the variable on the space for some odd reason. any thoughts would be appreciated!

Best,

EDIT - my example failed to recreate the issue. For reference, temp is being created in my code by scraping code from online with rvest, and for some reason it must be scraping a different character other than a normal space, i think? I need to split these strings by space though.

like image 307
Canovice Avatar asked Oct 19 '25 03:10

Canovice


1 Answers

Try the following:

unlist(strsplit(temp, "\\s+"))

The "\\s+" is a regex search for any type of whitespace instead of just a standard space.

like image 53
ode2k Avatar answered Oct 21 '25 16:10

ode2k



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!