[R] Replace / with - in date

arnaud Gaboury arnaud.gaboury at gmail.com
Fri Apr 16 08:44:09 CEST 2010


Why don't you try something like :

Xd$x=as.date(xd$x,format="%y/%m/%d").





> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Christian Raschke
> Sent: Thursday, April 15, 2010 8:28 PM
> To: r-help at r-project.org
> Subject: Re: [R] Replace / with - in date
> 
> Is there anything that speaks against just applying gsub to the factor
> levels if one would like to keep everything as factors (and not
> consider
> true Date classes or character vectors)? I.e:
> 
>  > x <- c("2000/01/01", "2001/02/01")
>  > xd <- as.data.frame(x)
>  > levels(xd$x) <- gsub("/", "-", levels(xd$x))
> 
> Christian
> 
> 
> On 04/15/2010 01:08 PM, David Winsemius wrote:
> >
> > On Apr 15, 2010, at 1:51 PM, prem_R wrote:
> >
> >>
> >> Hi,every one .I have searched the solutions in the forum  for
> >> replacing my
> >> date value which is in a data frame  ,01/01/2000 to 01-01-2000 using
> >> replace
> >> function but got the following warning message
> >> x<-"2000/01/01"
> >> xd<-as.data.frame(x)
> >> xd$x<-replace(xd$x,xd$x=="/","-")
> >
> > The replace function does not work with factors, it works with
> > (complete) vectors, not substrings. It's also a real hassle to do
> such
> > operations on factors, so just use character vectors and try gsub
> > instead:
> >
> > > x<-"2000/01/01"
> > > xd<-as.data.frame(x, stringsAsFactors=FALSE)
> > > xd$x2<-gsub("/","-", xd$x)
> > > xd
> >            x         x2
> > 1 2000/01/01 2000-01-01
> >
> >>
> >>
> >> Warning message:
> >> In `[<-.factor`(`*tmp*`, list, value = "-") :
> >>  invalid factor level, NAs generated
> >>
> >> Is there any other method of doing it? or am i missing something?.
> >> please
> >> let me know if you need any more information.
> >>
> >> Thanks.
> >>
> >> Prem
> >> --
> >> View this message in context:
> >> http://n4.nabble.com/Replace-with-in-date-tp1911391p1911391.html
> >> Sent from the R help mailing list archive at Nabble.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.
> >
> > David Winsemius, MD
> > West Hartford, CT
> >
> > ______________________________________________
> > 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.
> 
> 
> --
> Christian Raschke
> Department of Economics
> and
> ISDS Research Lab (HSRG)
> Louisiana State University
> Patrick Taylor Hall, Rm 2128
> Baton Rouge, LA 70803
> crasch2 at lsu.edu
> 
> ______________________________________________
> 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.



More information about the R-help mailing list