Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

r - 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)

My 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

What I want :

                              from                        to
 1                               UK                    Benin
 2                          Nantes                     Widha
 3                          London                    France
 4                         America                   America
 5                    La Martinique                    London

Can I combine transmute_all (or transmute_if) (package dplyr) and split (package tidyr) functions in dplyr pipe ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

Or in base R with lapply.

df[] <- lapply(network, function(x) sub(",.*", "", x))

data

Reading data as characters by using stringsAsFactors = FALSE.

network <- data.frame(from, to, stringsAsFactors = FALSE)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...