@@ -51,16 +51,29 @@ setGlobalInspectionID <- function(
51
51
data = inspections , column = columns [3L ], hhmm = default.time , seed = 123L
52
52
)
53
53
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 )
57
57
58
- updated <- setInspectionId(inspections , observations , inspection_ids )
58
+ # Set inspection ids
59
+ INSPNO <- " inspno"
60
+ INSPID <- " inspection_id"
59
61
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
+ ))
64
77
)
65
78
}
66
79
@@ -125,44 +138,58 @@ stop_on_hash_duplicates <- function(hashes, error.file = NULL)
125
138
}
126
139
}
127
140
128
- # setInspectionId ---- ----------------------------------------------------------
141
+ # replaceInspectionId ----------------------------------------------------------
129
142
130
- # ' Add column inspection_id to table of inspections and observations
143
+ # ' Replace values in columns inspection_id
131
144
# '
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}.
140
156
# ' @importFrom kwb.utils moveColumnsToFront removeColumns selectColumns
141
157
# ' @export
142
158
# ' @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
+ # ' )
147
168
# ' )
148
- # ' setInspectionId(inspections, observations, paste0("id_", 1:3))
169
+ # ' replaceInspectionId(inspection.data, new_ids = paste0("id_", 1:3))
149
170
# '
150
- setInspectionId <- function (inspections , observations , inspection_ids )
171
+ replaceInspectionId <- function (inspection.data , new_ids )
151
172
{
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 ))
154
178
155
179
INSPID <- " inspection_id"
156
- INSPNO <- " inspno"
157
180
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 ))
161
187
162
- id_first <- function (x ) kwb.utils :: moveColumnsToFront(x , INSPID )
188
+ observations [[INSPID ]] <- new_ids [indices ]
189
+ inspections [[INSPID ]] <- new_ids
163
190
164
191
list (
165
- inspections = id_first( inspections ),
166
- observations = id_first( kwb.utils :: removeColumns( observations , INSPNO ))
192
+ inspections = inspections ,
193
+ observations = observations
167
194
)
168
195
}
0 commit comments