首页>Program>source
Country<-c(" Germany"," France", "Greece"," United-State", "Spain")
Country

例如在 Germany之前有空格 , FranceUnited-State

[1] " Germany"      " France"       "Greece"        " United-State" "Spain"

我想删除国家名称前的所有空格,以便具有以下内容:

[1] "Germany"      "France"       "Greece"        "United-State" "Spain"
6

r

最新回答
  • 7天前
    1 #

    我们可以使用 trimws 来自 base R (未使用任何软件包)

    Country <- trimws(Country)
    Country
    #[1] "Germany"      "France"       "Greece"       "United-State" "Spain"      
    

    或与 sub

    sub("^\\s+", "", Country)
    

  • 7天前
    2 #

    这是使用 str_trim的解决方案 从 stringr 包,以便从字符串的开头和结尾删除空格。

    Code:

    library(stringr)
    Country<-c(" Germany"," France", "Greece"," United-State", "Spain")
    str_trim(Country)
    

    Output:

    [1] "Germany"      "France"       "Greece"       "United-State" "Spain"
    

    Original:

    [1] " Germany"      " France"       "Greece"        " United-State" "Spain"   
    

    由reprex软件包(v0.3.0)于2020-11-21创建

  • r:cortest()的p值与人工值不同吗?
  • java:Gui可视化递归回溯数独