Find the next date in a series by group

I have several such data:

sample.data <- rbind(data.table(start.date=seq(from=as.Date("2010-01-01"), to=as.Date("2014-12-01"), by="quarter"),
                 Group=c("A","B","C","D"), rnorm(20, 5)),
                 data.table(start.date=seq(from=as.Date("2010-01-01"), to=as.Date("2014-12-01"), by="quarter"),
                 Group=c("A","B","C","D"), rnorm(20, 3))
                 )

I would like to create a column end.datethat will be equal to the next early value start.datefor each group.

So, for example, the first start.datefor Group==A- 2010-01-01. The next earliest start.datefor Group==A- 2011-01-01. Thus, the final result should look like this if sorted by Group:

                start.date Group   end.date
                2010-01-01     A 2011-01-01
                2010-01-01     A 2011-01-01
                2011-01-01     A 2012-01-01
                2011-01-01     A 2012-01-01
                2012-01-01     A 2013-01-01
                2012-01-01     A 2013-01-01
                2013-01-01     A 2014-01-01
                2013-01-01     A 2014-01-01
                2014-01-01     A         NA
                2014-01-01     A         NA
                2010-04-01     B 2011-04-01
                2010-04-01     B 2011-04-01
                2011-04-01     B 2012-04-01
                2011-04-01     B 2012-04-01

Etc. Ideally, I would like to do this by reference, for example

sample.data[, end.date := EXPRESSION]

but I don’t understand where to start. Thanks for any help.

+4
source share
3 answers

So good:

events = unique(sample.data[ , .(Group, start.date) ])
events[, next.date := shift(start.date, type="lead"), by=Group]

sample.data[events, on=c("Group", "start.date"), end.date := next.date ]

-, OP events /tidy data.

> sample.data[ order(Group, start.date) ]

    start.date Group   end.date
 1: 2010-01-01     A 2011-01-01
 2: 2010-01-01     A 2011-01-01
 3: 2011-01-01     A 2012-01-01
 4: 2011-01-01     A 2012-01-01
 5: 2012-01-01     A 2013-01-01
 6: 2012-01-01     A 2013-01-01
 7: 2013-01-01     A 2014-01-01
 8: 2013-01-01     A 2014-01-01
 9: 2014-01-01     A       <NA>
10: 2014-01-01     A       <NA>
11: 2010-04-01     B 2011-04-01
12: 2010-04-01     B 2011-04-01
...
+5

dplyr :

require(dplyr); require(magrittr)
sample.data %<>%
    group_by(Group) %>% 
    mutate(end.date = sort(start.date, decreasing = FALSE)[2]) %>%

sort [n], , .

.

sample.data %<>%
    arrange(Group, start.date) %>%
    group_by(Group) %>%
    mutate(end.date2 = sort(start.date, decreasing = FALSE)[row_number(Group) + 2]) %>% 
    arrange(Group)

    >> head(sample.data, n = 4)
Source: local data frame [4 x 4]
Groups: Group [1]

  start.date Group       V3  end.date2
      <date> <chr>    <dbl>     <date>
1 2010-01-01     A 4.899328 2011-01-01
2 2010-01-01     A 3.451904 2011-01-01
3 2011-01-01     A 5.779825 2012-01-01
4 2011-01-01     A 4.182594 2012-01-01
+2

1) Group start.date sort(unique(start.date)) :

sample.data[, end.date := {u <- sort(unique(start.date)); u[match(start.date, u) + 1]}, 
  by = Group]

2) ave, - :

transform(sample.data, end.date = ave(start.date, Group, FUN = 
   function(x) { u <- unique(sort(x)); u[match(x, u) + 1] }))
+2
source

Source: https://habr.com/ru/post/1659210/


All Articles