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

  • Home
  • SEARCH
  • 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 6930369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:26:17+00:00 2026-05-27T11:26:17+00:00

I have dataframe that was created from the fusion of two dataframes. Both spanned

  • 0

I have dataframe that was created from the fusion of two dataframes. Both spanned over the same time intervall but contained different information. When I put them together, the info overlapped since there is no holes in the time interval of one of the dataframe. Here is an example where the rows “sp=A and B” are part of a first df and the rows “sp=C” come from a second. The first dataframe is continuous but the second consists of sporadic events. The resulting dataframe looks like this:

start                  end                         sp
2010-06-01 17:00:00    2010-06-01 19:30:00         A
2010-06-01 19:30:01    2010-06-01 20:00:00         B
2010-06-01 19:45:00    2010-06-01 19:55:00         C
2010-06-01 20:00:01    2010-06-01 20:30:00         A
2010-06-01 20:05:00    2010-06-01 20:10:00         C
2010-06-01 20:12:00    2010-06-01 20:15:00         C
2010-06-01 20:30:01    2010-06-01 20:40:00         B
2010-06-01 20:35:00    2010-06-01 20:40:10         C
2010-06-01 20:40:01    2010-06-01 20:50:00         A

I would like to prioritize “C” so when it overlaps the time interval of another “sp”, the time interval of “A” or “B” is cut accordingly. As seen in the example, I sometimes have multiple events of “C” that overlap a single event of “A” or “B”. The result would be this:

start                  end                         sp
2010-06-01 17:00:00    2010-06-01 19:30:00         A
2010-06-01 19:30:01    2010-06-01 19:44:59         B
2010-06-01 19:45:00    2010-06-01 19:55:00         C
2010-06-01 19:55:01    2010-06-01 20:00:00         B
2010-06-01 20:00:01    2010-06-01 20:04:59         A
2010-06-01 20:05:00    2010-06-01 20:10:00         C
2010-06-01 20:10:01    2010-06-01 20:11:59         A
2010-06-01 20:12:00    2010-06-01 20:15:00         C
2010-06-01 20:15:01    2010-06-01 20:30:00         A
2010-06-01 20:30:01    2010-06-01 20:34:59         B
2010-06-01 20:35:00    2010-06-01 20:40:10         C
2010-06-01 20:40:11    2010-06-01 20:50:00         A 

My date/time columns are in POSIXct. Don’t hesitate to ask if something is unclear.

Thanks in advance

  • 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-27T11:26:18+00:00Added an answer on May 27, 2026 at 11:26 am

    Here’s a nice way to do this with the plyr package and a recursive function:

    library(plyr)
    
    splitTimes <- function(arow, df) {
      overlap_all    = arow$start > df[, 'start'] & arow$end < df[, 'end']
      overlap_middle = arow$start < df[, 'start'] & arow$end > df[, 'end']
      overlap_end    = arow$start < df[, 'start'] & arow$end > df[, 'start'] & arow$end < df[, 'end']
      overlap_start  = arow$start > df[, 'start'] & arow$end > df[, 'end'] & arow$start < df[, 'end']
    
      if(any(overlap_all)) {
        data.frame()
      } else if(any(overlap_middle)) {
        outrows = rbind(data.frame(start=arow$start, end=df[overlap_middle, 'start'][1]-1, sp=arow$sp),
                        data.frame(start=df[overlap_middle, 'end'][1]+1, end=arow$end, sp=arow$sp))
        ddply(outrows, 'start', 'splitTimes', df)
      } else if(any(overlap_end)) {
        data.frame(start=arow$start, end=df[overlap_end, 'start']-1, sp=arow$sp)
      } else if(any(overlap_start)) {
        data.frame(start=df[overlap_start, 'end']+1, end=arow$end, sp=arow$sp)
      } else {
        arow
      }
    }
    

    Then you can do:

    > dfall = read.table('data.txt', header=T, colClasses=c('POSIXct', 'POSIXct', 'factor'))
    
    > dfAB = subset(dfall, sp %in% c('A', 'B'))
    > dfC  = subset(dfall, sp == 'C')
    
    > arrange(rbind(ddply(dfAB, 'start', 'splitTimes', dfC), dfC), start)
                     start                 end sp
    1  2010-06-01 17:00:00 2010-06-01 19:30:00  A
    2  2010-06-01 19:30:01 2010-06-01 19:44:59  B
    3  2010-06-01 19:45:00 2010-06-01 19:55:00  C
    4  2010-06-01 19:55:01 2010-06-01 20:00:00  B
    5  2010-06-01 20:00:01 2010-06-01 20:04:59  A
    6  2010-06-01 20:05:00 2010-06-01 20:10:00  C
    7  2010-06-01 20:10:01 2010-06-01 20:11:59  A
    8  2010-06-01 20:12:00 2010-06-01 20:15:00  C
    9  2010-06-01 20:15:01 2010-06-01 20:30:00  A
    10 2010-06-01 20:30:01 2010-06-01 20:34:59  B
    11 2010-06-01 20:35:00 2010-06-01 20:40:10  C
    12 2010-06-01 20:40:11 2010-06-01 20:50:00  A
    

    which gives you exactly what you want.

    There might be some small bugs in the other cases, since your example data set doesn’t cover them all, but this is the general idea at least. Hope it helps. Good luck!

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

Sidebar

Related Questions

I have two dataframes and I wish to insert the values of one dataframe
I have a dataframe with a column of integers that I would like to
I have a data frame in R that has come about from running some
I have created a data frame from a SQL query using the RODBC package.
I have a dataframe with the lengths and widths of various arthropods from the
I want to create a single shape file from multiple mxd's that have multiple
I've got a nice facet_wrap density plot that I have created with ggplot2 .
I need to create a new dataframe from rows from dataframe1, such that the
I have a list of dataframes for which I am certain that they all
I have a time critical application that needs to send a UDP datagram on

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.