-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathLocalOps.R
294 lines (259 loc) · 10.6 KB
/
LocalOps.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# in-memory direct functionality
#' @importFrom stats as.formula
NULL
#' @export
#' @rdname build_pivot_control
build_pivot_control.default <- function(table,
columnToTakeKeysFrom,
columnToTakeValuesFrom,
...,
prefix = columnToTakeKeysFrom,
sep = NULL,
tmp_name_source = wrapr::mk_tmp_name_source("bpcd"),
temporary = TRUE) {
wrapr::stop_if_dot_args(substitute(list(...)), "cdata::build_pivot_control")
if(!is.data.frame(table)) {
stop("build_pivot_control.default table should be a data.frame")
}
controlTable <- data.frame(vals = unique(table[[columnToTakeKeysFrom]]),
stringsAsFactors = FALSE)
colnames(controlTable) <- columnToTakeKeysFrom
controlTable[[columnToTakeKeysFrom]] <- as.character(controlTable[[columnToTakeKeysFrom]])
controlTable[[columnToTakeValuesFrom]] <- controlTable[[columnToTakeKeysFrom]]
if(!is.null(sep)) {
controlTable[[columnToTakeValuesFrom]] <- paste(prefix,
controlTable[[columnToTakeValuesFrom]],
sep=sep)
}
controlTable
}
#' Build a rowrecs_to_blocks() control table that specifies a un-pivot (or "shred").
#'
#' Some discussion and examples can be found here:
#' \url{https://winvector.github.io/FluidData/FluidData.html} and
#' here \url{https://github.com/WinVector/cdata}.
#'
#' @param nameForNewKeyColumn character name of column to write new keys in.
#' @param nameForNewValueColumn character name of column to write new values in.
#' @param columnsToTakeFrom character array names of columns to take values from.
#' @param ... not used, force later args to be by name
#' @return control table
#'
#' @seealso \code{\link{rowrecs_to_blocks}}
#'
#' @examples
#'
#' build_unpivot_control("measurmentType", "measurmentValue", c("c1", "c2"))
#'
#' @export
build_unpivot_control <- function(nameForNewKeyColumn,
nameForNewValueColumn,
columnsToTakeFrom,
...) {
wrapr::stop_if_dot_args(substitute(list(...)), "cdata::build_unpivot_control")
controlTable <- data.frame(x = as.character(columnsToTakeFrom),
y = as.character(columnsToTakeFrom),
stringsAsFactors = FALSE)
colnames(controlTable) <- c(nameForNewKeyColumn, nameForNewValueColumn)
controlTable
}
#' @export
#' @rdname rowrecs_to_blocks
rowrecs_to_blocks.default <- function(wideTable,
controlTable,
...,
checkNames = TRUE,
checkKeys = FALSE,
strict = FALSE,
controlTableKeys = colnames(controlTable)[[1]],
columnsToCopy = NULL,
tmp_name_source = wrapr::mk_tmp_name_source("rrtobd"),
temporary = TRUE,
allow_rqdatatable = FALSE) {
wrapr::stop_if_dot_args(substitute(list(...)), "cdata::rowrecs_to_blocks")
if(!is.data.frame(wideTable)) {
stop("cdata::rowrecs_to_blocks.default wideTable should be a data.frame")
}
check_rowrecs_to_blocks_args(wideTable_columns = colnames(wideTable),
controlTable = controlTable,
checkNames = checkNames,
strict = strict,
controlTableKeys = controlTableKeys,
columnsToCopy = columnsToCopy)
rownames(wideTable) <- NULL
controlTableValueColumns <- setdiff(colnames(controlTable), controlTableKeys)
# check more
if(checkKeys) {
if(!check_cols_form_unique_keys(wideTable, columnsToCopy)) {
stop("cdata::rowrecs_to_blocks columnsToCopy do not uniquely key the rows")
}
}
# see if it is an obvious simple unpivot
if(allow_rqdatatable &&
(ncol(controlTable)==2) &&
requireNamespace("rqdatatable", quietly = TRUE) &&
(isTRUE(all.equal(controlTable[ ,1, drop = TRUE],
controlTable[ ,2, drop = TRUE]))) &&
(controlTableKeys == colnames(controlTable)[[1]])) {
res <- rqdatatable::layout_to_blocks_data_table(
data = wideTable,
nameForNewKeyColumn = colnames(controlTable)[[1]],
nameForNewValueColumn = colnames(controlTable)[[2]],
columnsToTakeFrom = controlTable[, 2, drop = TRUE],
columnsToCopy = columnsToCopy)
res <- data.frame(res)
rownames(res) <- NULL
return(res)
}
# do the work
n_row_in <- nrow(wideTable)
n_rep <- nrow(controlTable)
n_row_res <- n_rep*n_row_in
# build and start filling in result
res <- data.frame(x = seq_len(n_row_in))
res[['x']] <- NULL
for(cn in columnsToCopy) {
res[[cn]] <- wideTable[[cn]]
}
for(cn in controlTableKeys) {
res[[cn]] <- NA_character_
}
for(cn in controlTableValueColumns) {
wtn <- wideTable[[controlTable[1, cn, drop = TRUE]]]
if(is.factor(wtn)) {
wtn <- as.character(wtn)
}
res[[cn]] <- wtn
res[[cn]][seq_len(n_row_in)] <- NA
}
# cross product with control table
res <- res[sort(rep(seq_len(n_row_in), n_rep)), , drop = FALSE] # TODO: speedup hotspot
rownames(res) <- NULL
for(cn in controlTableKeys) {
res[[cn]] <- rep(controlTable[[cn]], n_row_in)
}
# fill in values
for(cn in controlTableValueColumns) {
res_cn <- res[[cn]]
for(i in seq_len(n_rep)) {
indxs <- i + n_rep*(0:(n_row_in-1))
col <- controlTable[i, cn, drop = TRUE]
wtni <- wideTable[[col]]
if(is.factor(wtni)) {
wtni <- as.character(wtni)
}
res_cn[indxs] <- wtni # TODO: speedup hotspot
}
res[[cn]] <- res_cn
}
rownames(res) <- NULL
res
}
#' @export
#' @rdname blocks_to_rowrecs
blocks_to_rowrecs.default <- function(tallTable,
keyColumns,
controlTable,
...,
columnsToCopy = NULL,
checkNames = TRUE,
checkKeys = FALSE,
strict = FALSE,
controlTableKeys = colnames(controlTable)[[1]],
tmp_name_source = wrapr::mk_tmp_name_source("btrd"),
temporary = TRUE,
allow_rqdatatable = FALSE) {
wrapr::stop_if_dot_args(substitute(list(...)), "cdata::blocks_to_rowrecs")
if(!is.data.frame(tallTable)) {
stop("cdata::blocks_to_rowrecs.default tallTable should be a data.frame")
}
check_blocks_to_rowrecs_args(tallTable_columns = colnames(tallTable),
keyColumns = keyColumns,
controlTable = controlTable,
columnsToCopy = columnsToCopy,
checkNames = checkNames,
strict = strict,
controlTableKeys = controlTableKeys)
rownames(tallTable) <- NULL
clear_key_column <- FALSE
if(length(keyColumns)<=0) {
# avoid no-keys case
tallTable$cdata_key_column <- 1
keyColumns <- "cdata_key_column"
clear_key_column <- TRUE
}
controlTableValueColumns <- setdiff(colnames(controlTable), controlTableKeys)
# check more
if(checkKeys) {
# check keyColumns plus controltable keys key data
if(!check_cols_form_unique_keys(tallTable, c(controlTableKeys, keyColumns))) {
stop(paste("cdata::blocks_to_rowrecs: controlTableKeys plus keyColumns do not uniquely index data"))
}
}
# see if it is an obvious simple unpivot
if(allow_rqdatatable &&
(ncol(controlTable)==2) &&
requireNamespace("rqdatatable", quietly = TRUE) &&
(isTRUE(all.equal(controlTable[ ,1, drop = TRUE],
controlTable[ ,2, drop = TRUE]))) &&
(controlTableKeys == colnames(controlTable)[[1]])) {
res <- rqdatatable::layout_to_rowrecs_data_table(
data = tallTable,
columnToTakeKeysFrom = colnames(controlTable)[[1]],
columnToTakeValuesFrom= colnames(controlTable)[[2]],
rowKeyColumns = keyColumns)
res <- data.frame(res)
rownames(res) <- NULL
return(res)
}
# do the work
# make simple grouping keys
tallTable$cdata_group_key_col <- 1
if(length(keyColumns)>=1) {
cols <- as.list(tallTable[ , keyColumns, drop=FALSE])
names(cols) <- NULL
keys <- do.call("paste", c(cols, sep = " CDATA_SEP "))
tallTable$cdata_group_key_col <- match(keys, keys)
tallTable <- tallTable[order(tallTable$cdata_group_key_col), , drop = FALSE]
}
first_idxs <- match(unique(tallTable$cdata_group_key_col), tallTable$cdata_group_key_col)
res <- tallTable[first_idxs,
c("cdata_group_key_col", keyColumns, columnsToCopy),
drop = FALSE]
rownames(res) <- NULL
n_res <- nrow(res)
# fill in values
tallTable$composite_meas_col <- do.call(paste,
c(as.list(tallTable[, controlTableKeys, drop = FALSE]),
list(sep = " CDATA_K_SEP ")))
controlTable$composite_meas_col <- do.call(paste,
c(as.list(controlTable[, controlTableKeys, drop = FALSE]),
list(sep = " CDATA_K_SEP ")))
n_rep <- nrow(controlTable)
for(i in seq_len(n_rep)) {
srccol <- controlTable$composite_meas_col[[i]]
indxs <- which(tallTable$composite_meas_col == srccol) # TODO: speedup hotspot
for(cn in controlTableValueColumns) {
destcol <- controlTable[[cn]][i]
vals <- tallTable[[cn]][indxs]
if(length(vals)>0) {
res[[destcol]] <- vals[[1]]
} else {
res[[destcol]] <- FALSE
}
res[[destcol]][seq_len(n_res)] <- NA
posns <- match(res$cdata_group_key_col,
tallTable$cdata_group_key_col[indxs])
lhs <- seq_len(n_res)
lhs <- lhs[!is.na(posns)]
posns <- posns[!is.na(posns)]
res[[destcol]][lhs] <- vals[posns]
}
}
res$cdata_group_key_col <- NULL
if(clear_key_column) {
res$cdata_key_column <- NULL
}
rownames(res) <- NULL
res
}