An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20041108/1ef24f22/attachment.pl
Converting strings to date
3 messages · João Mendes Moreira, Brian Ripley, Roger D. Peng
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
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/