user_n
user_n

Reputation: 53

Delete data with gaps

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.

Upvotes: 0

Views: 100

Answers (2)

jeremycg
jeremycg

Reputation: 24945

using dplyr:

library(dplyr)
data3 %>% group_by(id) %>%
          filter(identical(time, seq(first(time), last(time))))

Upvotes: 0

David Robinson
David Robinson

Reputation: 78610

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]

Upvotes: 2

Related Questions