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

2016-05-30 Thread Jim Lemon
Hi Jun, As you do seem to want to replace commas within, not between, strings, try gsub: gsub(",",";",test[,1]) Jim > 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

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

2016-05-30 Thread Leonardo Ferreira Fontenelle
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 re

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

2016-05-27 Thread Ulrik Stervbo
If your data.frame is a mix you can loop over each column - along the lines of: library(plyr) adply(test, 2, function(x){ if(!is.numeric(x[[1]]){ gsub(",", ";", x[[1]]) }else{ x[[1]] } }) Ulrik On Fri, 27 May 2016 at 17:21 Jun Shen wrote: > Thanks Ulrik and Bob for your reply. > > gsub worked

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

2016-05-27 Thread David L Carlson
sage- From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Jun Shen Sent: Friday, May 27, 2016 10:21 AM To: Ulrik Stervbo Cc: R-help Subject: Re: [R] How to replace all commas with semicolon in a string Thanks Ulrik and Bob for your reply. gsub worked for one column! If I want to rep

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

2016-05-27 Thread Jun Shen
Thanks Ulrik and Bob for your reply. gsub worked for one column! If I want to replace the whole data frame, gsub doesn't seem to work. Any idea On Fri, May 27, 2016 at 11:14 AM, Ulrik Stervbo wrote: > use gsub() > > On Fri, 27 May 2016 at 17:12 Jun Shen wrote: > >> Dear list, >> >> Say I have

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

2016-05-27 Thread Ulrik Stervbo
use gsub() On Fri, 27 May 2016 at 17:12 Jun Shen wrote: > 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

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

2016-05-27 Thread boB Rudis
You can use gsub() instead of sub() On Fri, May 27, 2016 at 11:10 AM, Jun Shen wrote: > 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 com