Dear R-ers! I have x as a variable in a data frame x. x<-data.frame(x=c("aa.bb","cc.dd.ee")) x$x<-as.character(x$x) x I am sorry for such a simple question - but how can I replace all periods in x$x with spaces? sub('.', ' ', x$x) - removes all letters to the left of each period... Thanks a lot for your advice! -- Dimitri Liakhovitski Ninah.com Dimitri.Liakhovitski at ninah.com
You need escape the period: gsub("\\.", " ", x$x) On Tue, Oct 13, 2009 at 2:26 PM, Dimitri Liakhovitski <ld7631 at gmail.com> wrote:> Dear R-ers! > > I have x as a variable in a data frame x. > > x<-data.frame(x=c("aa.bb","cc.dd.ee")) > x$x<-as.character(x$x) > x > > I am sorry for such a simple question - but how can I replace all > periods in x$x with spaces? > > sub('.', ' ', x$x) - removes all letters to the left of each period... > > Thanks a lot for your advice! > > -- > Dimitri Liakhovitski > Ninah.com > Dimitri.Liakhovitski at ninah.com > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
As you've discovered, the . means something special in regular expressions (and R's version of them). You need to escape it with \\:> x<-data.frame(x=c("aa.bb","cc.dd.ee")) > x$x<-as.character(x$x) > xx 1 aa.bb 2 cc.dd.ee> sub("\\.", " ", x$x)[1] "aa bb" "cc dd.ee"> gsub("\\.", " ", x$x)[1] "aa bb" "cc dd ee" And to change all, you need gsub() rather than sub(). Sarah On Tue, Oct 13, 2009 at 1:26 PM, Dimitri Liakhovitski <ld7631 at gmail.com> wrote:> Dear R-ers! > > I have x as a variable in a data frame x. > > x<-data.frame(x=c("aa.bb","cc.dd.ee")) > x$x<-as.character(x$x) > x > > I am sorry for such a simple question - but how can I replace all > periods in x$x with spaces? > > sub('.', ' ', x$x) - removes all letters to the left of each period... > > Thanks a lot for your advice! >-- Sarah Goslee http://www.functionaldiversity.org
Here is one more that works: gsub("."," ","Start.Time", fixed = TRUE) "fixed = TRUE" really helps in a lot of instances for removing specific characters without accidently angering the regular expression gods. Enjoy. --- On Tue, 10/13/09, Dimitri Liakhovitski <ld7631 at gmail.com> wrote:> From: Dimitri Liakhovitski <ld7631 at gmail.com> > Subject: [R] replacing period with a space > To: "R-Help List" <r-help at stat.math.ethz.ch> > Date: Tuesday, October 13, 2009, 12:26 PM > Dear R-ers! > > I have x as a variable in a data frame x. > > x<-data.frame(x=c("aa.bb","cc.dd.ee")) > x$x<-as.character(x$x) > x > > I am sorry for such a simple question - but how can I > replace all > periods in x$x with spaces? > > sub('.', ' ', x$x) - removes all letters to the left of > each period... > > Thanks a lot for your advice! > > -- > Dimitri Liakhovitski > Ninah.com > Dimitri.Liakhovitski at ninah.com > > ______________________________________________ > R-help at r-project.org > mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, > reproducible code. >
Try chartr:> chartr(".", " ", "a.b.c")[1] "a b c" On Tue, Oct 13, 2009 at 1:26 PM, Dimitri Liakhovitski <ld7631 at gmail.com> wrote:> Dear R-ers! > > I have x as a variable in a data frame x. > > x<-data.frame(x=c("aa.bb","cc.dd.ee")) > x$x<-as.character(x$x) > x > > I am sorry for such a simple question - but how can I replace all > periods in x$x with spaces? > > sub('.', ' ', x$x) - removes all letters to the left of each period... > > Thanks a lot for your advice! > > -- > Dimitri Liakhovitski > Ninah.com > Dimitri.Liakhovitski at ninah.com > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Possibly Parallel Threads
- transforming a badly organized data base into a list of data frames
- referring to a row number and to a row condition, and to columns simultaneously
- Analogue to SPSS regression commands ENTER and REMOVE in R?
- Code is too slow: mean-centering variables in a data frame by subgroup
- Efficiency question: replacing all NAs with a zero