Message-ID: <4190C61B.6060907@jhsph.edu>
Date: 2004-11-09T13:28:59Z
From: Roger D. Peng
Subject: Converting strings to date
In-Reply-To: <009701c4c5c3$b39bc000$5e7aa8c0@FEUPsig.fe.up.pt>
You need to be more specific when you say "it works" =) strptime()
returns a POSIXlt object whose printed representation appears to be a
string (but it is a list). What you want, I think, is either
as.POSIXct() or as.Date(). See ?DateTimeClasses.
-roger
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?
>
> Thanks
>
> Joao Moreira
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
--
Roger D. Peng
http://www.biostat.jhsph.edu/~rpeng/