Quantcast
Channel: StackExchange Replication Questions
Viewing all articles
Browse latest Browse all 17268

Counting Week Data - Replicate, Transform and Percent Agreggation

$
0
0

I need to transform some data from a specific initial and end date to weekdays or days counting.

https://i.stack.imgur.com/aBOSF.png to this https://i.stack.imgur.com/KqS3L.png

Example: Code table

structure(list(year.month = structure(c(1L, 2L, 2L, 2L, 2L), .Label = c("2016-05", 
"2016-08"), class = "factor"), id = 1:5, begin = structure(c(2L, 
1L, 3L, 5L, 4L), .Label = c("02/07/2016", "13/04/2016", "24/07/2016", 
"25/07/2016", "30/06/2016"), class = "factor"), end = structure(c(1L, 
2L, 2L, 2L, 2L), .Label = c("01/05/2016", "01/08/2016"), class = "factor"), 
    value = c(4L, 7L, 3L, 4L, 6L)), .Names = c("year.month", 
"id", "begin", "end", "value"), class = "data.frame", row.names = c(NA, 
-5L))

For each row, I need to calculate weeks that hapenned to be in the specific begin and end time. This way , begin date 13-04 and end date 01-05 passed between the weeks starting in 11-04, 18-04 and 25-04, each of them with different contributions, first with 5 days (wed-dom), second with 7 days (seg-dom) and third with 7 days (seg-dom)

For each week, also i need to calculate the percent of days compared to total period of that id.

I manage to do this but only creating a loop for each row (using strftime and as.POSIXlt) and this gets extremely slow for a big dataset (several hours). Is there another way of doing this by matrix commands or transforming the whole data at once?

Thanks,


Viewing all articles
Browse latest Browse all 17268

Trending Articles



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