R字符串操作

一、删除字符串中的空格

基础包中的trimws函数。

用法

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)

你可能感兴趣的:(R字符串操作)