r - 转换所有列:删除逗号和逗号后的每个字符
问题描述
我想删除所有列中字符串中逗号后的逗号和每个字符
from <- c("UK, port unspecified", "Nantes", "London", "America", "La Martinique, port unspecified")
to <- c("Benin", "Widha", "France, *", "America, Port unspecified", "London")
network <- data.frame(from, to)
我的 df :
from to
1 UK, port unspecified Benin
2 Nantes Widha
3 London France, *
4 America America, Port unspecified
5 La Martinique, port unspecified London
我想要的是 :
from to
1 UK Benin
2 Nantes Widha
3 London France
4 America America
5 La Martinique London
我可以在 dplyr 管道中组合transmute_all
(或transmute_if
)(包 dplyr)和split
(包 tidyr)功能吗?
解决方案
您可以使用mutate_all
/transmute_all
并使用 . 删除逗号后的所有内容sub
。
library(dplyr)
network %>% mutate_all(~sub(",.*", "", .))
# from to
#1 UK Benin
#2 Nantes Widha
#3 London France
#4 America America
#5 La Martinique London
或者在带有lapply
.
df[] <- lapply(network, function(x) sub(",.*", "", x))
数据
使用 . 将数据读取为字符stringsAsFactors = FALSE
。
network <- data.frame(from, to, stringsAsFactors = FALSE)
推荐阅读
- java - Referencing a join table with no code in a hibernate query
- javascript - 如何在页面加载后激活 li
- python - 使用 IMDB 数据集查询交叉表
- swift - 使用正则表达式在字符串之间快速提取字符串
- vue.js - 如何使 vuetify 自动完成组件相互独立
- postman - 如何使用 API 调用 (POSTMAN) 查看用户角色
- machine-learning - 批量大小为 1 的实例规范化
- python - Herokuapp 无法编译
- doxygen - 让 doxygen 接受一个项目的无文档(但不要忽略它)
- php - 背包文件上传失败 Laravel 验证