Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 204876

List dates between two dates and divide the amount associated with new date to respective dates and id R/PYTHON/EXCEL

$
0
0

My data looks like this. I counted the difference between check in date and check out date to get number of days and based on days I will divide "Value" to each respective date. For ex. ID 3222808 had a stay of 2 days, so I will divide value 458/2 and assign it to each date. How do I do that in R or excel.

ID  Value   Checkin Checkout
3222808 458 11/15/2019  11/17/2019
606203  238 10/28/2019  10/30/2019
334229  218 11/16/2019  11/18/2019
837066  218 11/16/2019  11/18/2019
620384  218 11/16/2019  11/18/2019
534690  218 11/16/2019  11/18/2019
3192449 298 11/16/2019  11/18/2019

df <- structure(list(ID = c(3222808L, 606203L, 334229L, 837066L, 620384L, 
  534690L, 3192449L), Value = c(458L, 238L, 218L, 218L, 218L, 218L, 
  298L), Checkin = structure(c(18215, 18197, 18216, 18216, 18216, 
  18216, 18216), class = "Date"), Checkout = structure(c(18217, 
  18199, 18218, 18218, 18218, 18218, 18218), class = "Date")),
  class = "data.frame", row.names = c(NA, -7L))

Output should look like:

ID  Value   Date 
3222808 229 11/15/2019  
3222808 229 11/16/2019

Code I am using currently is giving me this -

library(dplyr)

pm <- df %>%
  rowwise() %>% 
  do(
    data.frame(.[1:2], date = seq(.$Checkin, .$Checkout, by = "1 day"))
  )

Patron  Value   date
3222808 458 11/15/2019
3222808 458 11/16/2019
3222808 458 11/17/2019
606203  238 10/28/2019
606203  238 10/29/2019
606203  238 10/30/2019
334229  218 11/16/2019

From here, I do not know how to group them and divide the amount.


Viewing all articles
Browse latest Browse all 204876

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>