gpt4 book ai didi

删除特定符号 ";"后的空格

转载 作者:行者123 更新时间:2023-12-05 00:48:50 25 4
gpt4 key购买 nike

我有一个关于删除列数据框内字 rune 本中的空格的问题。这是我的数据框列:

head(data$HO)
[1] "Lidar; Wind field; Temperature; Aerosol; Fabry-Perot etalon"
[2] "Compressive ghost imaging; Guided filter; Single-pixel imaging"

这个问题与这个问题不同 link因为我只想删除符号“;”之后的空格,所以输出应该是这样的:
head(data$HO)
[1] "Lidar;Wind field;Temperature;Aerosol;Fabry-Perot etalon"
[2] "Compressive ghost imaging;Guided filter;Single-pixel imaging"

我试过了
data$HO <- gsub("\\;s", ";",data$HO)

但它不起作用。

有什么建议吗?

最佳答案

您可以使用 ;\s+模式并替换为 ; :

> x <- c("Lidar; Wind field; Temperature; Aerosol; Fabry-Perot etalon", "Compressive ghost imaging; Guided filter; Single-pixel imaging")
> gsub(";\\s+", ";", x)
[1] "Lidar;Wind field;Temperature;Aerosol;Fabry-Perot etalon"
[2] "Compressive ghost imaging;Guided filter;Single-pixel imaging"

图案详情:
  • ; - 分号
  • \s+ - 一个或多个空白字符。

  • regex demo .

    解决方案的更多变体:
    gsub("(*UCP);\\K\\s+", "", x, perl=TRUE)
    gsub(";[[:space:]]+", ";", x)

    关于删除特定符号 ";"后的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48631884/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com