我注意到函式 strsplit() 和 scan() 處理下劃線的方式不同,我想知道為什么會這樣。
請考慮以下示例代碼:
x1 <- "string split"
strsplit(x1, " ")[[1]]
# [1] "string" "split"
scan(text = x1, what = " ")
# [1] "string" "split"
當使用“”作為分隔符時,strsplit 和 scan 的輸出是相同的。
但是,當我使用“_”作為分隔符時,輸出是不同的:
x2 <- "string_split"
strsplit(x2, "_")[[1]]
# [1] "string" "split"
scan(text = x2, what = "_")
# [1] "string_split"
使用下劃線作為分隔符時,為什么 strsplit 和 scan 的輸出不同?
uj5u.com熱心網友回復:
引數what不是分隔符,而是資料型別。在你的情況下,你有性格:
scan(text=x2, what = character(), sep='_', quiet = TRUE)
[1] "string" "split"
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/404649.html
標籤:
