[R] Applying strptime() to a data set or array

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Mar 7 08:23:37 CET 2006


The simplest way to do this is to use colClasses to specify that you want
POSIXct: see the documentation for read.table.

strptime returns an object of class POSIXlt (a list of length 9), and you 
cannot put that in a data frame.  So the issue is not that stated in the 
subject line: apply strptime to a character vector (or factor) and you get 
a list.

If you look at the code,

> strptime
function (x, format, tz = "")
.Internal(strptime(as.character(x), format, tz))

there is already an as.character, so factors do work.

And to reiterate, when you print A$date you see thousands of character 
strings (not rows), but that does not tell you that the internal 
representation is a character vector.  When you print a data frame, you 
may see thousands of rows but the length is the number of columns.

On Tue, 7 Mar 2006, Jacques VESLOT wrote:

> it's probably a factor, not a string vector...
> so i would do as.vector or as.character - but it may be not necessary !
>
> as.POSIXct(strptime(as.vector(A$date),...))
>
> or:
> seq( from = ISOdate(2006,02,28, 12, 0, 0, tz=""),
>     to = ISOdate(...), by="30 min")
>
>
> Andrew Athan a écrit :
>
>>
>> A$date is already a string, as read from the file.  I tried it anyway,
>> for you...
>>
>>> A$date<-strptime(as.character(A$date),"%Y-%m-%d %H:%M:%s")
>> Error in "$<-.data.frame"(`*tmp*`, "date", value = list(sec = c(0, 0,  :
>>        replacement has 9 rows, data has 3198
>>>
>>
>> A.
>>
>> Jacques VESLOT wrote:
>>
>>> maybe you need to transform A$date to character:
>>> A$date <- strptime(as.character(A$date), ...)
>>>
>>> see also:
>>> ?ISOdatetime
>>>
>>> Andrew Athan a écrit :
>>>
>>>> I'm sure this is just the result of a basic misunderstanding of the
>>>> syntax of R, but I am stumped.
>>>>
>>>> A <-
>>>> read.table(file="sumByThirtyMinute.csv",sep=",",col.names=c("date","pandl"))
>>>>
>>>>
>>>> A now consists of thousands of rows, but A$date is a string...
>>>> ...
>>>> 3183 2006-02-28 12:00:00    548.470
>>>> 3184 2006-02-28 12:30:00    515.240
>>>> 3185 2006-02-28 13:00:00    140.120
>>>> 3186 2006-02-28 13:30:00    450.940
>>>> 3187 2006-02-28 14:00:00    318.570
>>>> ...
>>>>
>>>>
>>>>
>>>>
>>>> So, I try to convert A$date to a POSIXlt ...
>>>>
>>>> A[,1]<-strptime(A$date,"%Y-%m-%d %H:%M:%s")
>>>> A$date<-strptime(A$date,"%Y-%m-%d %H:%M:%s")
>>>>
>>>>
>>>> which gives me a warning that the length of the array I am trying to
>>>> replace A$date with is 9 ... but if I print
>>>> strptime(A$date,"%Y-%m-%d %H:%M:%s"), it clearly has thousands of
>>>> rows.  Yet, if I ask for length(strptime(A$date,"%Y-%m-%d
>>>> %H:%M:%s")), I get 9.
>>>>
>>>> What am I doing wrong?  Do I need to convert the return value of
>>>> strptime(A$date,"%Y-%m-%d %H:%M:%s") to some array/vector/matrix
>>>> datatype before attempting to assign it?
>>>>
>>>> Thanks,
>>>> Andrew
>>>>
>>>> ______________________________________________
>>>> 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
>>>>
>>>>
>>>>
>>>
>>
>
> ______________________________________________
> 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
>

-- 
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