0

I want to delete data with gaps between the max and min time period corresponding to an individual id. Each Id can start and end in any time period, that is fine. I just want to grab ids that do not have missing time within the max and min time.

library(data.table)
set.seed(5)
data<-data.table(y=rnorm(100))
data[sample(1:100, 40),]<-NA
id = rep(1:10, each = 10)
time = seq(1,10)
data2<-data.frame(id,time)
data2$row<-1:nrow(data2)
data2a<-subset(data2,row<55|row>61 )
data3<-data2a[-sample(nrow(data2a), 5),]
data.table(data3)
count(data3$id)

Here is a good example. Group 1 should be deleted, but not 6 for example.

ekad
  • 14,436
  • 26
  • 44
  • 46
user_n
  • 53
  • 7
  • 3
    For future reference, there's never a reason to delete a question or its title because it's answered (that would prevent it from being useful to future searchers). – David Robinson Aug 13 '15 at 03:16

2 Answers2

2

The condition you want to filter for is that there are no gaps greater than 1. diff(time) gives you the gaps, so all(diff(time) == 1) checks the condition.

You can thus do this with:

library(dplyr)
data3 %>%
    group_by(id) %>%
    filter(all(diff(time) == 1))

In data.table, one solution (that does the same thing) is:

setDT(data3)[, .SD[all(diff(time) == 1)], id]
David Robinson
  • 77,383
  • 16
  • 167
  • 187
0

using dplyr:

library(dplyr)
data3 %>% group_by(id) %>%
          filter(identical(time, seq(first(time), last(time))))
jeremycg
  • 24,657
  • 5
  • 63
  • 74