一、刪除字符串中的空格
基礎包中的trimws
函數(shù)艘绍。
用法
trimws(x, which = c("both", "left", "right"), whitespace = "[ \t\r\n]")
- which指定空格的位置。
- whitespace指定空格的操作符病毡。
實例
library(tidyverse)
//生成一個tribble表
df <- tribble(
~x1,
"a, b, c",
"d, e, f, g"
)
//將X1列字符串拆分类缤,并解嵌套
df <- df %>%
mutate(x2=str_split(x1, ",")) %>%
unnest()
//將x2中的空格刪去
df$x2 <- trimws(df$x2)