From df635bbff5649c8438bacecb106ae9c4ff57eb97 Mon Sep 17 00:00:00 2001 From: hsonne Date: Fri, 22 Nov 2024 21:05:27 +0100 Subject: [PATCH] setInspectionId() -> replaceInspectionId() --- NAMESPACE | 2 +- R/setGlobalInspectionID.R | 93 ++++++++++++++++++++++++-------------- man/replaceInspectionId.Rd | 41 +++++++++++++++++ man/setInspectionId.Rd | 34 -------------- 4 files changed, 102 insertions(+), 68 deletions(-) create mode 100644 man/replaceInspectionId.Rd delete mode 100644 man/setInspectionId.Rd diff --git a/NAMESPACE b/NAMESPACE index 8979579..7c4f17e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -9,7 +9,7 @@ export(plotObservations) export(readAndMergeEuCodedFiles) export(readEuCodedFile) export(readEuCodedFiles) -export(setInspectionId) +export(replaceInspectionId) export(writeEuCodedFile) export(writeEuCodedFiles) importFrom(kwb.utils,allAreEqual) diff --git a/R/setGlobalInspectionID.R b/R/setGlobalInspectionID.R index c9d64a7..056d718 100644 --- a/R/setGlobalInspectionID.R +++ b/R/setGlobalInspectionID.R @@ -51,16 +51,29 @@ setGlobalInspectionID <- function( data = inspections, column = columns[3L], hhmm = default.time, seed = 123L ) - # Create inspection_ids and check if they have duplicated values - inspection_ids <- createHashFromColumns(inspections, columns, silent = TRUE) - stop_on_hash_duplicates(inspection_ids, error.file = error.file) + # Create new inspection_ids and check if they have duplicated values + new_ids <- createHashFromColumns(inspections, columns, silent = TRUE) + stop_on_hash_duplicates(new_ids, error.file = error.file) - updated <- setInspectionId(inspections, observations, inspection_ids) + # Set inspection ids + INSPNO <- "inspno" + INSPID <- "inspection_id" - list( - header.info = get_elements(inspection.data, "header.info"), - inspections = updated$inspections, - observations = updated$observations + if (! INSPNO %in% names(inspections)) { + inspections[[INSPNO]] <- seq_len(nrow(inspections)) + } + + use_inspection_id <- function(x) { + x[[INSPID]] <- kwb.utils::selectColumns(x, INSPNO) + kwb.utils::moveColumnsToFront(kwb.utils::removeColumns(x, INSPNO), INSPID) + } + + c( + list(header.info = get_elements(inspection.data, "header.info")), + replaceInspectionId(new_ids = new_ids, inspection.data = list( + inspections = use_inspection_id(inspections), + observations = use_inspection_id(observations) + )) ) } @@ -125,44 +138,58 @@ stop_on_hash_duplicates <- function(hashes, error.file = NULL) } } -# setInspectionId -------------------------------------------------------------- +# replaceInspectionId ---------------------------------------------------------- -#' Add column inspection_id to table of inspections and observations +#' Replace values in columns inspection_id #' -#' @param inspections data frame where each row represents an inspection -#' @param observations data frame where each row represents an observation. The -#' data frame must have a column "inspno" that refers to a row in the data -#' frame \code{inspections}. -#' @param inspection_ids vector of as many inspection ids as there are rows in -#' \code{inspections} -#' @return list with elements \code{inspections} and \code{observations} each of -#' which has a new column "inspection_id" as its first column. +#' @param inspection.data list with inspections data frame in element +#' \code{inspections} and observations data frame in element +#' \code{observations}. Both data frames must have a column +#' \code{inspection_id}. +#' @param new_ids vector of as many inspection ids as there are rows in +#' \code{inspection.data$inspections} to be given to the inspections in that +#' data frame. The first element is given to the first row, the second to the +#' second row, and so on. +#' @return list with data frames in elements \code{inspections} and +#' \code{observations}. In each data frame the values in column +#' \code{inspection_id} are updated according to the \code{new_ids}. #' @importFrom kwb.utils moveColumnsToFront removeColumns selectColumns #' @export #' @examples -#' inspections <- data.frame(pipe_id = 1:3) -#' observations <- data.frame( -#' inspno = c(1, 1, 2, 2, 3, 3), -#' observation = c("start", "end", "start", "end", "start", "end") +#' inspection.data <- list( +#' inspections = data.frame( +#' inspection_id = 1:3, +#' pipe_id = 1:3 +#' ), +#' observations = data.frame( +#' inspection_id = c(1, 1, 2, 2, 3, 3), +#' observation = c("start", "end", "start", "end", "start", "end") +#' ) #' ) -#' setInspectionId(inspections, observations, paste0("id_", 1:3)) +#' replaceInspectionId(inspection.data, new_ids = paste0("id_", 1:3)) #' -setInspectionId <- function(inspections, observations, inspection_ids) +replaceInspectionId <- function(inspection.data, new_ids) { - stopifnot(length(inspection_ids) == nrow(inspections)) - stopifnot(!anyDuplicated(inspection_ids)) + inspections <- get_elements(inspection.data, "inspections") + observations <- get_elements(inspection.data, "observations") + + stopifnot(length(new_ids) == nrow(inspections)) + stopifnot(!anyDuplicated(new_ids)) INSPID <- "inspection_id" - INSPNO <- "inspno" - inspection_numbers <- kwb.utils::selectColumns(observations, INSPNO) - inspections[[INSPID]] <- inspection_ids - observations[[INSPID]] <- inspection_ids[inspection_numbers] + indices <- match( + kwb.utils::selectColumns(observations, INSPID), + kwb.utils::selectColumns(inspections, INSPID) + ) + + stopifnot(!anyNA(indices)) - id_first <- function(x) kwb.utils::moveColumnsToFront(x, INSPID) + observations[[INSPID]] <- new_ids[indices] + inspections[[INSPID]] <- new_ids list( - inspections = id_first(inspections), - observations = id_first(kwb.utils::removeColumns(observations, INSPNO)) + inspections = inspections, + observations = observations ) } diff --git a/man/replaceInspectionId.Rd b/man/replaceInspectionId.Rd new file mode 100644 index 0000000..0b99b14 --- /dev/null +++ b/man/replaceInspectionId.Rd @@ -0,0 +1,41 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/setGlobalInspectionID.R +\name{replaceInspectionId} +\alias{replaceInspectionId} +\title{Replace values in columns inspection_id} +\usage{ +replaceInspectionId(inspection.data, new_ids) +} +\arguments{ +\item{inspection.data}{list with inspections data frame in element +\code{inspections} and observations data frame in element +\code{observations}. Both data frames must have a column +\code{inspection_id}.} + +\item{new_ids}{vector of as many inspection ids as there are rows in +\code{inspection.data$inspections} to be given to the inspections in that +data frame. The first element is given to the first row, the second to the +second row, and so on.} +} +\value{ +list with data frames in elements \code{inspections} and + \code{observations}. In each data frame the values in column + \code{inspection_id} are updated according to the \code{new_ids}. +} +\description{ +Replace values in columns inspection_id +} +\examples{ +inspection.data <- list( + inspections = data.frame( + inspection_id = 1:3, + pipe_id = 1:3 + ), + observations = data.frame( + inspection_id = c(1, 1, 2, 2, 3, 3), + observation = c("start", "end", "start", "end", "start", "end") + ) +) +replaceInspectionId(inspection.data, new_ids = paste0("id_", 1:3)) + +} diff --git a/man/setInspectionId.Rd b/man/setInspectionId.Rd deleted file mode 100644 index 13a7010..0000000 --- a/man/setInspectionId.Rd +++ /dev/null @@ -1,34 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/setGlobalInspectionID.R -\name{setInspectionId} -\alias{setInspectionId} -\title{Add column inspection_id to table of inspections and observations} -\usage{ -setInspectionId(inspections, observations, inspection_ids) -} -\arguments{ -\item{inspections}{data frame where each row represents an inspection} - -\item{observations}{data frame where each row represents an observation. The -data frame must have a column "inspno" that refers to a row in the data -frame \code{inspections}.} - -\item{inspection_ids}{vector of as many inspection ids as there are rows in -\code{inspections}} -} -\value{ -list with elements \code{inspections} and \code{observations} each of - which has a new column "inspection_id" as its first column. -} -\description{ -Add column inspection_id to table of inspections and observations -} -\examples{ -inspections <- data.frame(pipe_id = 1:3) -observations <- data.frame( - inspno = c(1, 1, 2, 2, 3, 3), - observation = c("start", "end", "start", "end", "start", "end") -) -setInspectionId(inspections, observations, paste0("id_", 1:3)) - -}