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 9221851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:40:14+00:00 2026-06-18T03:40:14+00:00

I have a data frame of 50 columns by 2.5 million rows in R,

  • 0

I have a data frame of 50 columns by 2.5 million rows in R, representing a time series. The time column is of class POSIXct. For analysis, I repeatedly need to find the state of the system for a given class at a particular time.

My current approach is the following (simplified and reproducible):

set.seed(1)
N <- 10000
.time <- sort(sample(1:(100*N),N))
class(.time) <- c("POSIXct", "POSIXt")
df <- data.frame(
  time=.time,
  distance1=sort(sample(1:(100*N),N)),
  distance2=sort(sample(1:(100*N),N)),
  letter=sample(letters,N,replace=TRUE)
)

# state search function
time.state <- function(df,searchtime,searchclass){
  # find all rows in between the searchtime and a while (here 10k seconds)
  # before that
  rows <- which(findInterval(df$time,c(searchtime-10000,searchtime))==1)
  # find the latest state of the given class within the search interval
  return(rev(rows)[match(T,rev(df[rows,"letter"]==searchclass))])
}  

# evaluate the function to retrieve the latest known state of the system
# at time 500,000.
df[time.state(df,500000,"a"),]

However, the call to which is very costly. Alternatively, I could first filter by class and then find the time, but that doesn’t change the evaluation time much. According to Rprof, it’s which and == that cost the majority of the time.

Is there a more efficient solution? The time points are sorted weakly increasing.

  • 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-06-18T03:40:15+00:00Added an answer on June 18, 2026 at 3:40 am

    Because which, == and [ are all linear with the size of the data frame, the solution is to generate subset data frames for bulk operations, as follows:

    # function that applies time.state to a series of time/class cominations
    time.states <- function(df,times,classes,day.length=24){
      result <- vector("list",length(times))
      day.end <- 0
      for(i in 1:length(times)){
        if(times[i] > day.end){
          # create subset interval from 1h before to 24h after
          day.begin <- times[i]-60*60
          day.end <- times[i]+day.length*60*60
          df.subset <- df[findInterval(df$time,c(day.begin,day.end))==1,]
        }
        # save the resulting row from data frame
        result[[i]] <- df.subset[time.state(df.subset,times[i],classes[i]),]
      }
      return(do.call("rbind",result))
    }
    

    With dT=diff(range(df$times)) and dT/day.length large, this reduces the evaluation time with a factor of dT/(day.length+1).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data frame with 22239 rows & 200 columns. The first column
Assuming in R, I have a data.frame with the first column representing the time
I have a data.frame with several columns (17). Column 2 have several rows with
I have a data frame with two columns. First column contains categories such as
I have a data frame with 50000 rows and 200 columns. There are duplicate
I have a data frame with an id column and some (potentially many) columns
I have a data frame that is some 35,000 rows, by 7 columns. it
Say I have a data frame with a bunch of columns. I need to
Suppose that you have a data frame with many rows and many columns. The
I have a data frame with 72 columns and 409 rows which is called:

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.