r - 在单词和数字之间替换逗号+空格
问题描述
我有:
x<- c("20% cotton 30% textile 50% other", "75.5% plastic 24.5% other")
我怎样才能将其更改为
c("20% cotton, 30% textile, 50% other", "75.5% plastic, 24.5% other")
?
gsub("[[:alpha:]] ", ", ", y) 不起作用,因为“吃”是单词中的最后一个符号。
解决方案
基于模式的一个选项是匹配一个或多个空格 ( +
) 后跟\\d
作为组 ( ) 捕获的数字 ( (...)
),并在 中replacement
插入,
后跟一个空格和\\1
捕获组的反向引用 ( )
gsub(" +(\\d)", ", \\1", x)
#[1] "20% cotton, 30% textile, 50% other" "75.5% plastic, 24.5% other"
推荐阅读
- javascript - 从鼠标单击事件侦听器添加对 Perl CGI 脚本的调用时出现问题
- javascript - React Native 嵌套扁平列表性能不佳
- c++ - 对象生命周期的结束与它何时停止存在之间的关系是什么?
- r - 在R中的表中合并多个保存为xls的html
- flutter - How to only change the time in DateTime?
- mysql - 在 phpmyadmin 中设置 int 类型长度无限制
- python - 使用多索引注释散点图
- github - GitHub App Web 应用程序流程:请求用户身份时的登录参数是什么?
- python - 无法设置虚拟环境或查看是否安装了 Anaconda,无法访问 bash 文件夹,“找不到命令”
- docker - 码头工人:standard_init_linux.go:211