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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:50:15+00:00 2026-06-05T15:50:15+00:00

I have a data frame with start dates and end dates, along with the

  • 0

I have a data frame with start dates and end dates, along with the number of people registered for an event. I would like to calculate the number of hours each party is present for within a specific timeframe (e.g., 07:00 – 17:00)

If I use the following example data.frame…

d <- data.frame(startDate = c(as.POSIXct("2011-06-04 08:00:00"), as.POSIXct("2011-06-03 08:00:00"),
                          as.POSIXct("2011-09-12 10:00:00")),
            endDate = c(as.POSIXct("2011-06-06 11:00:00"), as.POSIXct("2011-06-04 11:00:00"),
                        as.POSIXct("2011-09-12 18:00:00")),
            partysize = c(124,442,323))
open <- "07:00"
close <- "17:00"

I would like my result set to look something like this:

day                     numhours  partysize
2011-06-04                     9        124
2011-06-05                    10        124
2011-06-06                     4        124
2011-06-03                     9        442
2011-06-04                     4        442
2011-09-12                     7        323

note: numhours is the number of hours the date was included between the open and close times

Thanks in advance,
–JT

  • 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-05T15:50:17+00:00Added an answer on June 5, 2026 at 3:50 pm

    Sorry its very messy and I used 7 and 17 instead of your open and close

    app.days<-mapply(function(x,y){x+y*60*60*24},as.POSIXct(format(d$startDate,"%Y-%m-%d")),lapply(floor(-(d$startDate-d$endDate)/24),seq,from=0))
    start.date<-mapply(function(x,y){pmax(x+7*60*60,y)},app.days,d$startDate)
    end.date<-mapply(function(x,y){pmin(x+17*60*60,y)},app.days,d$endDate)
    app.hours<-mapply(function(x,y){as.numeric(x-y)},end.date,start.date)
    res<-mapply(function(x,y,z){data.frame(day=as.Date(x),numhours=y,partysize=z)},app.days,app.hours,as.list(d$partysize))
    res1<-data.frame(day=as.Date(unlist(res[1,]),origin="1970-01-01"),numhours=unlist(res[2,]),partysize=unlist(res[3,]))
    
    > res1
             day numhours partysize
    1 2011-06-04        9       124
    2 2011-06-05       10       124
    3 2011-06-06        4       124
    4 2011-06-03        9       442
    5 2011-06-04        4       442
    6 2011-09-12        7       323
    

    Basically we identify how many days each party size stays for. For a given day we find the applicable open and close. Then we subtract open from close. The dataframe is eventually formed but it could probably have been created in the res<- step…..

    • 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 of a time series of data, I would like to
I have a dataframe in R like this: dat = data.frame(Sample = c(1,1,2,2,3), Start
I have a dataframe that looks like this: person n start end 1 sam
I have a data frame like below (20,000 rows by 49 cols). Each row
I have a data frame with a number of infections identified from clinical isolates
I have the following data set which shows the start and the end of
For on the dataframe AB: AB<-data.frame(ID=c(1,2,4),A=c(2,8,8),B=c(6,2,2),dE=c(0,0,0)) I would like to apply the following formula:
I have data frame with some numerical variables and some categorical factor variables. The
I have a data frame with two qualitative variables (Q1, Q2) which are both
I have a data frame that is some 35,000 rows, by 7 columns. it

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.