Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6601087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:42:28+00:00 2026-05-25T18:42:28+00:00

I’ve written a function that takes a data.frame which represent intervals of data which

  • 0

I’ve written a function that takes a data.frame which represent intervals of data which occur across a 1 minute timeframe. The purpose of the function is to take these 1 minute intervals and convert them into higher intervals. Example, 1 minute becomes 5 minute, 60 minute etc…The data set itself has the potential to have gaps in the data i.e. jumps in time so it must accommodate for these bad data occurrences. I’ve written the following code which appears to work but the performance is absolutely terrible on large data sets.

I’m hoping that someone could provide some suggestions on how I might be able to speed this up. See below.

compressMinute = function(interval, DAT) {
    #Grab all data which begins at the same interval length
    retSet = NULL
    intervalFilter = which(DAT$time$min %% interval == 0)
    barSet = NULL
    for (x in intervalFilter) {
        barEndTime = DAT$time[x] + 60*interval
        barIntervals = DAT[x,]
        x = x+1
        while(x <= nrow(DAT) & DAT[x,"time"] < barEndTime) {
            barIntervals = rbind(barIntervals,DAT[x,])
            x = x + 1
        }
        bar = data.frame(date=barIntervals[1,"date"],time=barIntervals[1,"time"],open=barIntervals[1,"open"],high=max(barIntervals[1:nrow(barIntervals),"high"]),
                        low=min(barIntervals[1:nrow(barIntervals),"low"]),close=tail(barIntervals,1)$close,volume=sum(barIntervals[1:nrow(barIntervals),"volume"]))
        if (is.null(barSet)) {
            barSet = bar
        } else {
            barSet = rbind(barSet, bar)
        }

    }
    return(barSet)
}

EDIT:

Below is a row of my data. Each row represents a 1 minute interval, I am trying to convert this into arbitrary buckets which are the aggregates of these 1 minute intervals, i.e. 5 minutes, 15 minutes, 60 minutes, 240 minutes, etc…

date                time    open    high     low   close volume
2005-09-06 2005-09-06 16:33:00 1297.25 1297.50 1297.25 1297.25     98
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:42:29+00:00Added an answer on May 25, 2026 at 6:42 pm

    You probably want to re-use existing facitlities, specifically the POSIXct time types, as well as existing packages.

    For example, look at the xts package — it already has a generic function to.period() as well as convenience wrappers to.minutes(), to.minutes3(), to.minutes10(), ….

    Here is an example from the help page:

    R> example(to.minutes)
    
    t.mn10R> data(sample_matrix)
    
    t.mn10R> samplexts <- as.xts(sample_matrix)
    
    t.mn10R> to.monthly(samplexts)
             samplexts.Open samplexts.High samplexts.Low samplexts.Close
    Jan 2007        50.0398        50.7734       49.7631         50.2258
    Feb 2007        50.2245        51.3234       50.1910         50.7709
    Mar 2007        50.8162        50.8162       48.2365         48.9749
    Apr 2007        48.9441        50.3378       48.8096         49.3397
    May 2007        49.3457        49.6910       47.5180         47.7378
    Jun 2007        47.7443        47.9413       47.0914         47.7672
    
    t.mn10R> to.monthly(sample_matrix)
             sample_matrix.Open sample_matrix.High sample_matrix.Low sample_matrix.Close
    Jan 2007            50.0398            50.7734           49.7631             50.2258
    Feb 2007            50.2245            51.3234           50.1910             50.7709
    Mar 2007            50.8162            50.8162           48.2365             48.9749
    Apr 2007            48.9441            50.3378           48.8096             49.3397
    May 2007            49.3457            49.6910           47.5180             47.7378
    Jun 2007            47.7443            47.9413           47.0914             47.7672
    
    t.mn10R> str(to.monthly(samplexts))
    An ‘xts’ object from Jan 2007 to Jun 2007 containing:
      Data: num [1:6, 1:4] 50 50.2 50.8 48.9 49.3 ...
     - attr(*, "dimnames")=List of 2
      ..$ : NULL
      ..$ : chr [1:4] "samplexts.Open" "samplexts.High" "samplexts.Low" "samplexts.Close"
      Indexed by objects of class: [yearmon] TZ: 
      xts Attributes:  
     NULL
    
    t.mn10R> str(to.monthly(sample_matrix))
     num [1:6, 1:4] 50 50.2 50.8 48.9 49.3 ...
     - attr(*, "dimnames")=List of 2
      ..$ : chr [1:6] "Jan 2007" "Feb 2007" "Mar 2007" "Apr 2007" ...
      ..$ : chr [1:4] "sample_matrix.Open" "sample_matrix.High" "sample_matrix.Low" "sample_matrix.Close"
    R> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to construct a data frame in an Rcpp function, but when I
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.