Skip to content

Commit df635bb

Browse files
committed
setInspectionId() -> replaceInspectionId()
1 parent 3e2dfe0 commit df635bb

File tree

4 files changed

+102
-68
lines changed

4 files changed

+102
-68
lines changed

NAMESPACE

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export(plotObservations)
99
export(readAndMergeEuCodedFiles)
1010
export(readEuCodedFile)
1111
export(readEuCodedFiles)
12-
export(setInspectionId)
12+
export(replaceInspectionId)
1313
export(writeEuCodedFile)
1414
export(writeEuCodedFiles)
1515
importFrom(kwb.utils,allAreEqual)

R/setGlobalInspectionID.R

+60-33
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,29 @@ setGlobalInspectionID <- function(
5151
data = inspections, column = columns[3L], hhmm = default.time, seed = 123L
5252
)
5353

54-
# Create inspection_ids and check if they have duplicated values
55-
inspection_ids <- createHashFromColumns(inspections, columns, silent = TRUE)
56-
stop_on_hash_duplicates(inspection_ids, error.file = error.file)
54+
# Create new inspection_ids and check if they have duplicated values
55+
new_ids <- createHashFromColumns(inspections, columns, silent = TRUE)
56+
stop_on_hash_duplicates(new_ids, error.file = error.file)
5757

58-
updated <- setInspectionId(inspections, observations, inspection_ids)
58+
# Set inspection ids
59+
INSPNO <- "inspno"
60+
INSPID <- "inspection_id"
5961

60-
list(
61-
header.info = get_elements(inspection.data, "header.info"),
62-
inspections = updated$inspections,
63-
observations = updated$observations
62+
if (! INSPNO %in% names(inspections)) {
63+
inspections[[INSPNO]] <- seq_len(nrow(inspections))
64+
}
65+
66+
use_inspection_id <- function(x) {
67+
x[[INSPID]] <- kwb.utils::selectColumns(x, INSPNO)
68+
kwb.utils::moveColumnsToFront(kwb.utils::removeColumns(x, INSPNO), INSPID)
69+
}
70+
71+
c(
72+
list(header.info = get_elements(inspection.data, "header.info")),
73+
replaceInspectionId(new_ids = new_ids, inspection.data = list(
74+
inspections = use_inspection_id(inspections),
75+
observations = use_inspection_id(observations)
76+
))
6477
)
6578
}
6679

@@ -125,44 +138,58 @@ stop_on_hash_duplicates <- function(hashes, error.file = NULL)
125138
}
126139
}
127140

128-
# setInspectionId --------------------------------------------------------------
141+
# replaceInspectionId ----------------------------------------------------------
129142

130-
#' Add column inspection_id to table of inspections and observations
143+
#' Replace values in columns inspection_id
131144
#'
132-
#' @param inspections data frame where each row represents an inspection
133-
#' @param observations data frame where each row represents an observation. The
134-
#' data frame must have a column "inspno" that refers to a row in the data
135-
#' frame \code{inspections}.
136-
#' @param inspection_ids vector of as many inspection ids as there are rows in
137-
#' \code{inspections}
138-
#' @return list with elements \code{inspections} and \code{observations} each of
139-
#' which has a new column "inspection_id" as its first column.
145+
#' @param inspection.data list with inspections data frame in element
146+
#' \code{inspections} and observations data frame in element
147+
#' \code{observations}. Both data frames must have a column
148+
#' \code{inspection_id}.
149+
#' @param new_ids vector of as many inspection ids as there are rows in
150+
#' \code{inspection.data$inspections} to be given to the inspections in that
151+
#' data frame. The first element is given to the first row, the second to the
152+
#' second row, and so on.
153+
#' @return list with data frames in elements \code{inspections} and
154+
#' \code{observations}. In each data frame the values in column
155+
#' \code{inspection_id} are updated according to the \code{new_ids}.
140156
#' @importFrom kwb.utils moveColumnsToFront removeColumns selectColumns
141157
#' @export
142158
#' @examples
143-
#' inspections <- data.frame(pipe_id = 1:3)
144-
#' observations <- data.frame(
145-
#' inspno = c(1, 1, 2, 2, 3, 3),
146-
#' observation = c("start", "end", "start", "end", "start", "end")
159+
#' inspection.data <- list(
160+
#' inspections = data.frame(
161+
#' inspection_id = 1:3,
162+
#' pipe_id = 1:3
163+
#' ),
164+
#' observations = data.frame(
165+
#' inspection_id = c(1, 1, 2, 2, 3, 3),
166+
#' observation = c("start", "end", "start", "end", "start", "end")
167+
#' )
147168
#' )
148-
#' setInspectionId(inspections, observations, paste0("id_", 1:3))
169+
#' replaceInspectionId(inspection.data, new_ids = paste0("id_", 1:3))
149170
#'
150-
setInspectionId <- function(inspections, observations, inspection_ids)
171+
replaceInspectionId <- function(inspection.data, new_ids)
151172
{
152-
stopifnot(length(inspection_ids) == nrow(inspections))
153-
stopifnot(!anyDuplicated(inspection_ids))
173+
inspections <- get_elements(inspection.data, "inspections")
174+
observations <- get_elements(inspection.data, "observations")
175+
176+
stopifnot(length(new_ids) == nrow(inspections))
177+
stopifnot(!anyDuplicated(new_ids))
154178

155179
INSPID <- "inspection_id"
156-
INSPNO <- "inspno"
157180

158-
inspection_numbers <- kwb.utils::selectColumns(observations, INSPNO)
159-
inspections[[INSPID]] <- inspection_ids
160-
observations[[INSPID]] <- inspection_ids[inspection_numbers]
181+
indices <- match(
182+
kwb.utils::selectColumns(observations, INSPID),
183+
kwb.utils::selectColumns(inspections, INSPID)
184+
)
185+
186+
stopifnot(!anyNA(indices))
161187

162-
id_first <- function(x) kwb.utils::moveColumnsToFront(x, INSPID)
188+
observations[[INSPID]] <- new_ids[indices]
189+
inspections[[INSPID]] <- new_ids
163190

164191
list(
165-
inspections = id_first(inspections),
166-
observations = id_first(kwb.utils::removeColumns(observations, INSPNO))
192+
inspections = inspections,
193+
observations = observations
167194
)
168195
}

man/replaceInspectionId.Rd

+41
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/setInspectionId.Rd

-34
This file was deleted.

0 commit comments

Comments
 (0)