How can I trim leading and trailing white space?

后端 未结 13 1483
北海茫月
北海茫月 2020-11-22 03:53

I am having some troubles with leading and trailing white space in a data.frame.

For example, I like to take a look at a specific row in a data.fra

13条回答
  •  伪装坚强ぢ
    2020-11-22 04:13

    Another related problem occurs if you have multiple spaces in between inputs:

    > a <- "  a string         with lots   of starting, inter   mediate and trailing   whitespace     "
    

    You can then easily split this string into "real" tokens using a regular expression to the split argument:

    > strsplit(a, split=" +")
    [[1]]
     [1] ""           "a"          "string"     "with"       "lots"
     [6] "of"         "starting,"  "inter"      "mediate"    "and"
    [11] "trailing"   "whitespace"
    

    Note that if there is a match at the beginning of a (non-empty) string, the first element of the output is ‘""’, but if there is a match at the end of the string, the output is the same as with the match removed.

提交回复
热议问题