首页 > 解决方案 > 转换所有列:删除逗号和逗号后的每个字符

问题描述

我想删除所有列中字符串中逗号后的逗号和每个字符

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)功能吗?

标签: rdplyrtidyr

解决方案


您可以使用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)

推荐阅读