String 字元/Sapce 去除多餘空格

  • 從網頁或者資料庫擷取下來的文字資料,常會發生左邊或者右邊有多餘的空格
  • R 語言
    • 使用 trimws() 函數清除文字中多餘的空格
    • 搭配 which 參數調整:
    • trimws(x, which = "left") :去除文字左邊的空格
    • trimws(x, which = "right") :去除文字右邊的空格
    • trimws(x, which = "both") :去除文字左邊與右邊的空格
shaq <- "     Shaquille O'Neal     "

trimws(shaq, which = "left")
## [1] "Shaquille O'Neal     "

trimws(shaq, which = "right")
## [1] "     Shaquille O'Neal"

trimws(shaq, which = "both")
## [1] "Shaquille O'Neal"

Reference:

results matching ""

    No results matching ""