You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何简洁可靠地将连续2个及以上空格替换为指定分隔符?

批量替换连续多空格为指定分隔符的简洁实现

原始数据

df <- structure(list(x1 = c("Greendale Village Wards 1 & 2                                     405     1,219               20                 5                 1", 
                            "Hales Corners Village Wards 4 - 6                                   400     1,002                7                 6                 2", 
                            "Kenosha City Ward 49                                                    0           0              0                 0                 0"
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -3L))

df
# A tibble: 3 × 1
  x1                                                                                                                                      
  <chr>                                                                                                                                    
1 Greendale Village Wards 1 & 2                                     405     1,219               20                 5                 1    
2 Hales Corners Village Wards 4 - 6                                   400     1,002                7                 6                 2  
3 Kenosha City Ward 49                                                    0           0              0                 0                 0

需求说明

需要将上述数据的x1列按≥2个连续空格拆分多列,第一步需把所有2个及以上的连续空格替换为单个指定分隔符(示例中用!!)。原实现方式需反复调用str_replace_all,操作繁琐且需手动确认调用次数,需更简洁可靠的实现方法。

原繁琐实现代码

df |>
  mutate(x1 = str_replace_all(x1, "  ", "!!")) |>
  mutate(x1 = str_replace_all(x1, "!!!", "!!")) |>
  mutate(x1 = str_replace_all(x1, "!!!", "!!")) |>
  mutate(x1 = str_replace_all(x1, "!!!", "!!")) |>
  mutate(x1 = str_replace_all(x1, "!!!", "!!")) |>
  mutate(x1 = str_replace_all(x1, "!!!", "!!")) |>
  mutate(x1 = str_replace_all(x1, "!!!", "!!")) |>
  mutate(x1 = str_replace_all(x1, "!!!", "!!")) |>
  mutate(x1 = str_replace_all(x1, "!!!", "!!")) |>
  mutate(x1 = str_replace_all(x1, "!!!", "!!"))
# A tibble: 3 × 1
  x1                                                        
  <chr>                                                     
1 Greendale Village Wards 1 & 2!! 405!! 1,219!! 20!! 5!! 1  
2 Hales Corners Village Wards 4 - 6!! 400!! 1,002!!7!! 6!! 2
3 Kenosha City Ward 49!!0!! 0!!0!! 0!! 0                    

简洁解决方案

使用正则表达式匹配2个及以上的连续空格,一次调用str_replace_all即可完成统一替换:

library(dplyr)
library(stringr)

df_clean <- df |>
  mutate(x1 = str_replace_all(x1, "\\s{2,}", "!!"))

df_clean
# A tibble: 3 × 1
  x1                                                        
  <chr>                                                     
1 Greendale Village Wards 1 & 2!!405!!1,219!!20!!5!!1      
2 Hales Corners Village Wards 4 - 6!!400!!1,002!!7!!6!!2   
3 Kenosha City Ward 49!!0!!0!!0!!0!!0                      

关键说明

  • 正则表达式\\s{2,}表示匹配2个或更多的空白字符,若需严格匹配空格而非所有空白符,可改用 {2,}
  • 单次替换即可完成所有连续多空格的转换,无需反复调用替换函数,高效且不易出错

如果需要直接拆分列,可继续使用separate函数:

df_split <- df_clean |>
  separate(x1, into = c("ward_name", "col1", "col2", "col3", "col4", "col5"), sep = "!!")

df_split
# A tibble: 3 × 6
  ward_name                          col1  col2   col3  col4  col5 
  <chr>                              <chr> <chr>  <chr> <chr> <chr>
1 Greendale Village Wards 1 & 2      405   1,219  20    5     1    
2 Hales Corners Village Wards 4 - 6  400   1,002  7     6     2    
3 Kenosha City Ward 49               0     0      0     0     0    

内容的提问来源于stack exchange,提问作者John J.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 00:57:04