[R] How to replace all commas with semicolon in a string

Leonardo Ferreira Fontenelle leonardof at leonardof.med.br
Mon May 30 19:50:48 CEST 2016


Em Sex 27 mai. 2016, às 12:10, Jun Shen escreveu:
> Dear list,
> 
> Say I have a data frame
> 
> test <- data.frame(C1=c('a,b,c,d'),C2=c('g,h,f'))
> 
> I want to replace the commas with semicolons
> 
> sub(',',';',test$C1) -> test$C1 will only replace the first comma of a
> string.
> 
> How do I replace them all in one run? Thanks.

If it's a CSV, you may read the file and then write it again (second
file name, for safety) with write.csv2.

HTH,

Leonardo Ferreira Fontenelle, MD, MPH
PhD candidate in epidemiology, Federal University of Pelotas
Professor of medicine, Vila Velha University
Legislative consultant in health, Municipal Chamber of Vitória



More information about the R-help mailing list