Friday 15 February 2013

r - Splitting a row at year change -


I have a large data set of data to represent the formed blocks of time, though I want a clean brake Fill in the same year the limitations of the year with the beginning and finishing of each line

See the table as an example below.

  Type duration cumsum year year.split 1 1 236 236 1 365 2 12 9 365 1 365 3 1 154 51 9 2 730 4 216 735 3 1095  

There is no overlap between the 3rd and the queue on the first day of the two years, even then the row starts 4 years in two years and it is three years away in 5 days. I want to divide the row 4 so that the table looks like the following.

  Type of period cumsum year year.split 1 1 236 236 1 365 2 0 12 9 365 1 365 3 1 0 51 9 1 365 4 1 154 51 9 2 730 5 211 524 2 730 6 5 5 735 3 10 9 5  

As it can be seen, there is no overlap in the whole year because every overlapping block of time has been divided into each row in the same year Starts and ends. The way I have done this, it is so far, though it seems friction and I hope that there will be a more elegant solution.

  set.seed (808) test & lt; - data.frame (type = c (1,0), period = round (ranif (20, min = 100, max = 250))%> mutate (cumsum = cumsum (period), year = roof (cumsum / 365), year (= year) * 365) Exam & lt; - RBIDID (test [1], filter (examination, end (year) == year), filter (testing, interval (year)! = Year)% & gt;% mutate (period = cumsum - (year -1) * 365), filter (test, interval (year)! = Year)%>% mutate (period = ((year 1) * 365 + Period-cumsum), cumsum = Cumsum period, year = year -1, year.split = year * 365))%> system% (year, cumsum) test & amp; By Muh_ (testing, type, year)% & gt;% short term (= sum) (period))% & gt;% Non-Group% & gt; The arrangement (year)  

summarizes the last two lines of code because I am interested in the total amount of each type.

What is a better way to do this?

It starts to work, assuming that all of these are strictly positive:

  cs & lt; -test $ cumsum cs0 & lt; -Sort (Unique (C: (1: Flooring (Max (CS) / 365)) * 365) Data Frame. (Type = trial $ type [search only (CS 0-0.5, CS) +1], duration = Difference (c (0, cs0)), cumsum = cs0, year = roof (cs0 / 365)) type period cumsum year 1 1 236 236 1 2 0 12 9 365 1 3 1 154 51 9 2 4 211 730 2 5 0 5 735 3  

No comments:

Post a Comment