[R] Converting strings to date

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Nov 8 21:18:16 CET 2004


You want as.Date, not strptime that gives you a *list*.

Please do read the help pages before posting, as the the posting guide
asks you to.

On Mon, 8 Nov 2004, João Mendes Moreira wrote:

> Hello,
> 
> I have the following problem:
> test is a data frame with 9 fields. The field test$Date is factorized with dates. The format is dd-mm-yyyy (using Oracle notation). I want to convert this to Date in '%Y-%m-%d format.
> What I am doing is:
> for (i in 1:nrow(test))
>   {
>    test[i,]$Data<-strptime(substring(test[i,]$Data,1,10),"%d-%m-%Y")
>   }
> 
> test is a data frame. 
> 
> The error is:
> Error in "$<-.data.frame"(`*tmp*`, "Data", value = list(sec = 0, min = 0,  : 
>         replacement has 9 rows, data has 1
> 
> But if I do:
> strptime(substring(test[1:nrow(teste),]$Data,1,10),"%d-%m-%Y")
> it works! Why the assignement does not work?

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list