Skip to content
Prev 391255 / 398500 Next

[EXTERNAL] Re: complicated time series filtering issue

Modifying the code from Kimmo slightly and embedding this in a looping process across individual organisms, here is script I ended up developing that does what I want .  I am sure there may be some more elegant solution but this worked.  Thank you all for the various suggestions.

Brian

?### To get prior dates and lengths by INDIVIDUAL with intervals between
### dates >10 days.
### Data set has dates in days as DATENUMBER, for INDIVIDUAL organisms,
### and lengths measured on those dates.

library(data.table)

master.data.dt <- data.table(master.data)

### Calculate intervals between dates by individual

master.data.lag <- master.data.dt[, DATENUMBER.prev:=c(NA, DATENUMBER[-.N]), by=INDIVIDUAL]

master.data.lag$interval <- master.data.lag$DATENUMBER - master.data.lag$DATENUMBER.prev

### to have a dummy dataframe row to rbind to.

master.data.newlag <- master.data.lag[1,]

### Begin looping across individuals, intervals set to be >=11 days

individlist <- names(table(master.data.lag$INDIVIDUAL))

for (i in seq(individlist)){
  temp.df <- master.data.lag[master.data.lag $INDIVIDUAL==individlist[i],]

j<-1
while (j<nrow(temp.df)) {
    if (temp.df$DATENUMBER[j+1]-temp.df$DATENUMBER[j]<11) {
        temp.df<-temp.df[-(j+1),]
    } else {
        temp.df$interval[j+1]<-temp.df$DATENUMBER[j+1]-temp.df$DATENUMBER[j]
        j<-j+1
    }
}
master.data.newlag <- rbind(master.data.newlag,temp.df)
}

### to eliminate the first dummy row created above

master.data.newlag <- master.data.newlag[-1,]

### to get lagged lengths and dates with the new intervals >10

master.data.newlag <- master.data.newlag[, length.prev:=c(NA, length[-.N]), by=INDIVIDUAL]

master.data.newlag <- master.data.newlag[, DATENUMBER.prev:=c(NA, DATENUMBER[-.N]), by=INDIVIDUAL]



Brian S. Cade, PhD

U. S. Geological Survey
Fort Collins Science Center
2150 Centre Ave., Bldg. C
Fort Collins, CO  80526-8818

email:  cadeb at usgs.gov<mailto:brian_cade at usgs.gov>
tel:  970 226-9326