rtime-serieslinear-regressionnacontiguous

In R, subset data using longest contiguous stretch of non-NA values


I am prepping data for linear regression and want to address missing values (NA) by using the longest contiguous stretch of non-NA values in a given year and site.

I have tried na.contiguous() but my code is not applying the function by year or site

Thanks for your assistance

The test data is a multivariate time series that spans 2 years and 2 sites. My hope is that the solution will accommodate data with many more years and 32 sites- so some level of automation and qa/qc is appreciated.


    library(dataRetrieval)
    library(dplyr)

    # read in Data, q is  discharge and wt is stream temperature
    wt<-readNWISdv(siteNumbers=c("08181800","07308500"),
    parameterCd=c("00010","00060"), statCd=c("00003"), 
    startDate="1998-07-01", endDate="1999-09-30" )
    dfwt<-wt%>%
      group_by(site_no)%>%
      select(Date,site_no,X_00010_00003,X_00060_00003)%>%
      rename(wt=X_00010_00003,q=X_00060_00003)

    #Subset summer season, add dummy air temp (at).

    dfwt$Date<-ymd(dfwt$Date, tz=Sys.timezone())
    dfwt$month<-month(dfwt$Date)
    dfwt$year<-year(dfwt$Date)
    df<- dfwt %>%
      group_by(site_no)%>%
      subset(month>=7 & month<=9)%>%
      mutate(at=wt*1.3) 
    # add NA
    df[35:38,3]<-NA
    df[155,3]<-NA
    df[194,3]<-NA

    test<-df%>%
      group_by(site_no, year)%>%
      na.contiguous(df) 


Solution

  • Using a for loop I found the following solution,

    library(zoo)
    library(plyr)
    library(lubridate)
    zoo(df)
    
    sites<-as.vector(unique(df$site_no))
    
    bfi_allsites<-data.frame()
    
    for(i in 1:2){
      
      site1<-subset(dfz, site_no==sites[i])
      str(site1)
      
      ss1<-split(site1,site1$year)
      site1result<-lapply(ss1,na.contiguous)#works
      site_df <- ldply(site1result,data.frame)
      bfi_allsites<-rbind(bfi_allsites, site_df)
    }
    head(bfi_allsites)