strsplit无法持续工作,字母之间的字符为';没有空间吗?
这个问题很简单,但我没办法解决。strsplit()是一个相当简单的函数,我感到惊讶的是,我和我一样挣扎:strsplit无法持续工作,字母之间的字符为';没有空间吗?,r,character,whitespace,R,Character,Whitespace,这个问题很简单,但我没办法解决。strsplit()是一个相当简单的函数,我感到惊讶的是,我和我一样挣扎: # 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
# 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"
.即使我在这里试图重现这个例子,但它不起作用,这就是我遇到的问题。使用temp,代码不会出于某种奇怪的原因在空间上拆分变量。任何想法都将不胜感激 最好的 编辑-我的示例无法重新创建问题。作为参考,temp是在我的代码中通过使用rvest从网上抓取代码创建的,我想,出于某种原因,它一定是在抓取普通空格以外的其他字符?我需要按空格分割这些字符串 尝试以下操作:
unlist(strsplit(temp, "\\s+"))
“\\s+”
是对任何类型的空格的正则表达式搜索,而不仅仅是一个标准空格。如注释所示
很可能“空格”实际上不是空格,而是其他一些空格字符。
尝试以下任一方法缩小范围:
whitespace <- c(" ", "\t" , "\n", "\r", "\v", "\f")
grep(paste(whitespace,collapse="|"), temp)
whitespace我可以用可复制的代码发布,但是这也会涉及到发布rvest()刮码,我不介意,但想看看我们是否可以在不首先发布的情况下找到解决方案当您执行grep(“,temp)”时会发生什么情况
?然后您可以尝试grep(“\t\n\r\v\f”,temp)
,查看这些空白字符是否有效。grep(“,temp)
返回integer(0)
您可以使用POSIX正则表达式查看类似神秘空格的字符,例如charToRaw
或utf8ToInt
也[:space:]+/code>