[R] Converting strings to date
Roger D. Peng
rpeng at jhsph.edu
Tue Nov 9 14:28:59 CET 2004
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/
More information about the R-help
mailing list