R/missing_values.R

Defines functions na_correction close_gaps

Documented in close_gaps na_correction

#' Close gaps in data
#'
#' Two functions exist, that can be used to close gaps in the `BioGasData` layer of a `BGF`.
#' Depending on the function, used either the last valid value of a fermentation is carried forward or values in between two measurements can be interpolated.
#'
#' The function `close_gaps` can be used to quickly close gaps at the end of a series of fermentations.
#' It will go through each 'time' in the `BioGasData` layer of a `BGF` and check if the corresponding 'product' and 'production' values are `NA`.
#' If `TRUE` the respective previous value will be selected and replaces the `NA`.
#' This action will be done specifically for each 'reactor' level.
#' It is called by  [from_AMPTSV2_report] when creating a `BGF` from an AMPTS II generated report.
#'
#' @param x a `BGF`
#' @param feedback `logic`. If `TRUE` the function will print a feedback to the console
#'
#' @returns a `BGF`
#'
#' @examples
#' # create an example BGF
#' myBGF <- BGF(
#'         ReactorLayout = c("2*Meso","Cellulose","2*S1 ctrl","2*S1 7d","2*S1 4d",
#'         "2*S2 ctrl","2*S2 4d","2*S2 6d"),
#'         BlankLabel = "Meso",
#'         name = "myBGF",
#'         ProcessTemp = 42,
#'         MeasurementType = "AMPTSV2")
#'
#' # add data generated by an AMPTS II
#' myBGF <- add_bmp_measurement(
#'         x = myBGF,
#'         path = base::system.file("extdata","AMPTSV2.csv",package = "bgfanalyzer"))
#'
#' # convert data columns
#' myBGF <- cols_to_numeric(myBGF)
#'
#' # close gaps in data
#' myBGF <- close_gaps(myBGF)
#'
#' @export
#'

# close_gaps() ####
close_gaps=function(x,feedback=FALSE){
  if(isFALSE(class(x)=="BGF")){ # check if 'x' is class basic_BGF
    stop("'x' must be class 'BGF'!",
         call. = FALSE)
  }

  tmax <- max(as.numeric(x$BioGasData$time),na.rm = T) # get final value  of 'x$BioGasData$time'

  for(i in c(1:length(x$BioGasData$time))){ # for each observation in 'x$BioGasData'
    lab=x$BioGasData$reactor[i] # get the reactor name in that obs
    now=x$BioGasData$time[i] #  and the value of 'time'
    value=x$BioGasData$product[i] # as well as the 'volume' value
    value2=x$BioGasData$production[i] # as well as the 'volume' value

    while(is.na(value)&&length(value)>0){ # check if value is 'NA'
      new_value <- subset(x$BioGasData,x$BioGasData$reactor==lab) # if this is TRUE, subset the 'BioGasData' to conatain only obs of the same reactor
      new_value <- subset(new_value,new_value$time==new_value$time[which(new_value$time ==now)-1]) # then further subset the data to have the previous obs
      new_value <- new_value$product # store the corresponding 'volume' value
      value=new_value # set value to the stored new value
    }

    while(is.na(value2)&&length(value2)>0){ # check if value2 is 'NA'
      new_value <- subset(x$BioGasData,x$BioGasData$reactor==lab) # if this is TRUE, subset the 'BioGasData' to conatain only obs of the same reactor
      new_value <- subset(new_value,new_value$time==new_value$time[which(new_value$time ==now)-1]) # then further subset the data to have the previous obs
      new_value <- new_value$production # store the corresponding 'production' value
      value2=new_value # set value to the stored new value
    }
    if(length(value)>0) x$BioGasData$product[i]=value # override the 'volume' value of  interest with the newly identified valid value
    if(length(value2)>0) x$BioGasData$production[i]=value2 # override the 'production' value of  interest with the newly identified valid value


  }

  # give feedback
  if(isTRUE(feedback)){ 
    m1 <-paste0("Closed gaps in imported volume data (",x$ExpParam$name,")...")
  
    message(m1)
    }

  return(x) # return modified x

}

#' @rdname close_gaps
#'
#' @details
#' The function `na_correction` can be used to close gaps in between or at the end of a fermentation.
#' Target data columns can be specified as `integer` or `character` in the `which` argument.
#' Alternatively, `which` can be 'all_num' (the default) to select all numeric columns in the `BioGasData`-layer of a `BGF`.
#' Internally the function calls [bgf_interpolation] on each column specified via the `which` argument.
#' Additional arguments passed to `na_correction` will be forwarded to [bgf_interpolation] as well.
#'
#'
#' @param which either a `numeric` or a `character`referring to `numeric` columns in the `BioGasData`-layer of a `BGF`. Can be 'all_num' (the default) to automatically select all `numeric` columns in the `BioGasData`-layer
#' @param ... further arguments passed to [bgf_interpolation]
#'
#' @examples
#' # another example BGF
#' myBGF2 <- from_standard_record(
#'         ReactorLayout = "A",
#'         ProcessTemp = 80,
#'         InocToSubRatio = .1,
#'         path = base::system.file("extdata","Fermentation_A.tsv",package = "bgfanalyzer"),
#'         time_col = 1,
#'         product_col = 3)
#'
#' # import gas quality measurements
#' gasq <- import_standard_record(
#'         ipath = base::system.file(
#'              "extdata",
#'              "gasq_A.tsv",
#'              package = "bgfanalyzer"),
#'         mkFRTime = "2025-01-15 17:00:00",
#'         FRTime_col = 1,
#'         units = "hours")
#'
#' # add gas quality data to BGF
#' myBGF2 <- add_BG_parameter(
#'         x = myBGF2,
#'         parameter = gasq,
#'         reactor= "R1",
#'         time = 3,
#'         value = 2,
#'         name = "H2",
#'         cut_zero = TRUE,
#'         interpolate_missing = FALSE)
#'
#' # ensure data structure integrity (recommended before using na_correction)
#' myBGF2 <- update_BGF(myBGF2)
#'
#' # close gaps resulting from merging fermentation data and gas quality data
#' # in all numerics of the BGF's 'BioGasData'-layer
#' myBGF2 <- na_correction(x=myBGF2)
#'
#' @export
#'

# na_correction() ####
na_correction=function(x,which="all_num",...){

  if(which=="all_num"){ # applies na_correct to all numeric columns
    which = names(x$BioGasData)
    which = subset(which,!which%in%c("reactor","time"))
    which=unlist(lapply(x$BioGasData[,which],is.numeric))
    which=subset(which,which==TRUE)
    which=names(which)

  }else{

    if(is.character(which)) which<-names(x$BioGasData)[match(as.character(which),names(x$BioGasData))]

    if(is.numeric(which)) which<-names(x$BioGasData)[which]

  }

  for(i in which){
    x$BioGasData<-bgf_interpolation(x$BioGasData,grep(paste0("\\b",{{i}},"\\b"),names(x$BioGasData)),grep("time",names(x$BioGasData)),grep("reactor",names(x$BioGasData)),...)

  }

  return(x) # return modified x
}

Try the bgfanalyzer package in your browser

Any scripts or data that you put into this service are public.

bgfanalyzer documentation built on Sept. 26, 2026, 5:07 p.m.