R/rename_attributes.R

Defines functions rename_attributes

Documented in rename_attributes

#' @title Rename data columns according to metadata.
#'
#' @description Rename data columns according to metadata.
#'
#' @param meta_list (character) A list of dataframes containing metadata returned by \code{\link{get_meta}}.
#' @param dataset_id (numeric) A dataset ID.
#' @param entity (numeric) An entity number.
#' @param file_dir (character) Path to directory containing flat files (data files). Defaults to current R working directory.
#' @param filename (character) Filename. Defaults to "", in which case the entity metadata will be read to find filename.
#' @param x R object. If specified, will skip looking for entity file altogether.
#' @param append_units (logical) If TRUE, will append abbreviated units (see vw_custom_units) to column names as specified in metabase. Defaults to TRUE.
#' @return The data table entity with renamed columns.
#' @importFrom data.table fread
#' @importFrom dplyr inner_join
#' @export


rename_attributes <-
  function(meta_list,
           dataset_id,
           entity,
           file_dir = getwd(),
           filename = "",
           x,
           append_units = T) {

    if (!is.null(x)) stopifnot(is.data.frame(x))

    # subset to specified dataset_id and entity number
    entity_e <-
      subset(meta_list[["entities"]], datasetid == dataset_id &
               entity_position == entity)

    # convert whitespace strings to NA for easy checking
    entity_e <- lapply(entity_e, stringr::str_trim)
    entity_e[entity_e == ""] <- NA
    entity_e <- as.data.frame(entity_e)

    attributes <-
      subset(meta_list[["attributes"]], datasetid == dataset_id &
               entity_position == entity)
    units <- subset(meta_list[["unit"]], datasetid == dataset_id)
    if (is.null(x)) {
    if (filename != "") {
    entity_df <- data.table::fread(file.path(file_dir, filename))
  } else entity_df <- data.table::fread(file.path(file_dir, entity_e[["filename"]]))
    } else entity_df <- x

    colnames(entity_df) <- attributes[["attributeName"]]
    return(entity_df)
  }
atn38/BLE-LTER-utils documentation built on Sept. 9, 2020, 7:49 p.m.