在所有列上转换:删除逗号和逗号后的每个字符 [英] transmute over all columns : removing comma and every characters after comma
本文介绍了在所有列上转换:删除逗号和逗号后的每个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在所有列中的字符串中删除逗号和逗号后的每个字符
I want to remove commas and every characters after commas in strings over all columns
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
我可以结合使用 transmute_all
(或 transmute_if
)(dplyr软件包)和<$ dplyr管道中的c $ c> split (软件包tidyr)功能?
Can I combine transmute_all
(or transmute_if
) (package dplyr) and split
(package tidyr) functions in dplyr pipe ?
推荐答案
您可以使用 mutate_all
/ transmute_all
,并使用 sub
删除逗号后的所有内容。 / p>
You can use mutate_all
/transmute_all
and remove everything after comma using 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
或者在底数为R的情况下愉快。
Or in base R with lapply
.
df[] <- lapply(network, function(x) sub(",.*", "", x))
数据
使用<$ c作为字符读取数据$ c> stringsAsFactors = FALSE 。
network <- data.frame(from, to, stringsAsFactors = FALSE)
这篇关于在所有列上转换:删除逗号和逗号后的每个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文