[R] Split data frame by date (POSIXlt)

MacQueen, Don macqueen1 at llnl.gov
Thu Aug 25 18:16:13 CEST 2011


I suspect, but have not tested, that your  src$date   element has class
"POSIXlt", which is internally a list, in which case splitting by it might
not work properly, and might be the cause of your out of bounds error
message.

One of these might do the job:
  src$date <- format(strptime(tickdata$date, "%d.%m.%Y"))
  src$date <- as.POSIXct(strptime(tickdata$date, "%d.%m.%Y"))

If you convert your dates to character strings in %Y-%m-%d format then
sorting will be in date order.

-Don

-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 8/24/11 3:11 PM, "Jean V Adams" <jvadams at usgs.gov> wrote:

>You could try using the numeric representation of date, and split the
>data 
>frame using that variable.  For example:
>
>src$date.num <- as.numeric(src$date)
>
>Jean
>
>Franc Lucas wrote on 08/24/2011 02:42:58 PM:
>> 
>>    Hello everyone,
>>    I want to split a data.frame by the column date . The data frame
>looks like
>>    this
>>    date                  time           open            close
>>    02.01.2011    09:00:00      1000             1200
>>    02.01.2011    09:05:02      1200             1203
>>    ...
>>    01.02.2011    10:01:21      1029             1110
>>    .....
>>    30.03.2011    12:02:12      1231              1200
>>    30.03.2011    17:00:00      1200              1190
>>    Please  note  that this is the German version of the date notation.
>So
>>    02.01.2011 is January 2nd 2011.
>>    So the column data is class: character.
>>    When I now split the dataframe by date, e.g.
>>    Intraday <- split(x=src, f=src$date, drop=FALSE)
>>    ..I get a list which is not sorted...for example:  "01.02.2011"
>(February
>>    1st) comes before "02.01.2011" (January 2nd).
>>    My approach was to transform the column date into POSIXct by using
>strptime
>>    (btw: I dont care for the time information):
>>    src$date <- strptime(tickdata$date, "%d.%m.%Y")
>>    The data frame then looks like this:
>>    date                  time           open            close
>>    01-02-2011    09:00:00      1000             1200
>>    01-02-2011    09:05:02      1200             1203
>>    ...
>>    02-01-2011    10:01:21      1029             1110
>>    .....
>>    03-30-2011    12:02:12      1231              1200
>>    03-30-2011    17:00:00      1200              1190
>>    which is totally fine. But when I now try to split the data frame it
>says,
>>    that  I  am  indexing out of bounds... (German: "Fehler in args[[i]]
>:
>>    Indizierung außerhalb der Grenzen")
>>    Can anybody help me?
>>    Thanks in advance!
>>    Best
>>    Franc
>>    BSc. Student
>>    University of Mannheim
>> 
>>    Schon gehört? WEB.DE hat einen genialen Phishing-Filter in die
>>    Toolbar eingebaut! [1]http://produkte.web.de/go/toolbar
>> 
>> References
>> 
>>    1. http://produkte.web.de/go/toolbar
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
>	[[alternative HTML version deleted]]
>



More information about the R-help mailing list