Vint
Vint

Reputation: 499

Inserting NA's during time gaps

I have a time series data set that I am trying to plot out, but the time series has large data gaps. When plotting this data set, R plots over these gaps with a straight line, and I'd prefer the final plot to not plot over these data gaps. The only way I know how to fix this is to manually insert a single row of NA between the gaps in the data set. To do this, I wrote up a function that loops through my dataframe. The function works, but is very slow to run.

#Define Function to Insert NA in long stretches so don't plot line 

PlotSpace<-function(DF){

NROW<-nrow(DF)
for(t in seq(1:NROW)){

TimeDiff<-difftime(DF$TimeStamp[t+1], DF$TimeStamp[t], units = "hours") 
DF[t,"TimeDiff"]<-TimeDiff  

if( !is.na(TimeDiff) & TimeDiff > 5 ){

    NewTimeStamp<-DF$TimeStamp[t] + 1
    NewProfStamp<-DF$ProfStamp[t] + 1
    print(NewTimeStamp)
    DF<-rbind(DF,NA) #Add last row that is NA
    DF[nrow(DF),'TimeStamp']<-NewTimeStamp
    DF[nrow(DF),'ProfStamp']<-NewProfStamp




    }
}

DF <- DF[order(DF$TimeStamp),]
DF<-DF[-1,]
DF$TimeStamp<-as.POSIXct(DF$TimeStamp)

return(DF)

}

Is there a more efficient way to do this in R?

Example Data:

 TimeStamp<-c("2015-05-01 10:00:00","2015-05-01 10:05:00","2015-05-01 10:10:00","2015-05-01 10:15:00",
"2015-05-01 10:20:00","2015-05-01 15:00:00","2015-05-01 15:05:00","2015-05-01 15:10:00"
,"2015-05-01 15:20:00","2015-05-01 15:30:00","2015-05-01 15:35:00")

Data<-c(1,2,3,4,5,3,7,8,9,2,11)

DF<-data.frame(TimeStamp, Data)

DF$TimeStamp<-as.POSIXct(DF$TimeStamp)

plot(DF$TimeStamp, DF$Data, type='l')

As you can see, the above graph plots out a line between the 7 hour data gap. I would like to insert an NA between anytime diff greater than 2 hours. AKA

             TimeStamp Data
1  2015-05-01 10:00:00    1
2  2015-05-01 10:05:00    2
3  2015-05-01 10:10:00    3
4  2015-05-01 10:15:00    4
5  2015-05-01 10:20:00    5
   2015-05-01 10:21:00    NA
6  2015-05-01 15:00:00    3
7  2015-05-01 15:05:00    7
8  2015-05-01 15:10:00    8
9  2015-05-01 15:20:00    9
10 2015-05-01 15:30:00    2
11 2015-05-01 15:35:00   11

Upvotes: 1

Views: 300

Answers (1)

IceCreamToucan
IceCreamToucan

Reputation: 28675

library(data.table)
setDT(DF)

# Create indicator for time gap
DF[, gap := c(diff(TimeStamp) > 2*60, F)]
# If there's a gap, add a new row
DF[, if(gap) rbind(.SD, .(TimeStamp = TimeStamp + 60), fill = T)
     else .SD
   , by = 1:nrow(DF)
   ][, -'gap']
#               TimeStamp Data
#  1: 2015-05-01 10:00:00    1
#  2: 2015-05-01 10:05:00    2
#  3: 2015-05-01 10:10:00    3
#  4: 2015-05-01 10:15:00    4
#  5: 2015-05-01 10:20:00    5
#  6: 2015-05-01 10:21:00   NA
#  7: 2015-05-01 15:00:00    3
#  8: 2015-05-01 15:05:00    7
#  9: 2015-05-01 15:10:00    8
# 10: 2015-05-01 15:20:00    9
# 11: 2015-05-01 15:30:00    2
# 12: 2015-05-01 15:35:00   11

Upvotes: 1

Related Questions