-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtransformer.R
More file actions
65 lines (54 loc) · 1.97 KB
/
transformer.R
File metadata and controls
65 lines (54 loc) · 1.97 KB
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
TransformationDMU <- function(table) {
if (nrow(table) == 0) {
return(data.table(DMU = "X")[FALSE, ])
}
table <- cbind(
TransformationDMUWindow(table),
TransformationPeriod(table)[, Formated := sprintf("%02d", Period)]
)
data.table(
DMU = paste(sep = "-", table$`DMU-window`, table$Formated)
)
}
TransformationDMUWindow <- function(table) {
if (nrow(table) == 0) {
return(data.table("DMU-window" = "X")[FALSE, ])
}
data.table(
"DMU-window" = ifelse(table$orderID == 0, "Hrac", paste0("Robot", table$orderID))
)
}
TransformationPeriod <- function(table) {
if (nrow(table) == 0) {
return(data.table("Period" = "X")[FALSE, ])
}
data.table(
"Period" = (1:nrow(table)-1) %/% 4 + 1
)
}
MakeSingleDataTransformation <- function(tranformation, table) {
switch(tranformation,
"#DMU" = TransformationDMU(table),
"#DMU-window" = TransformationDMUWindow(table),
"#Period" = TransformationPeriod(table),
stop("XXX"))
}
MakeAllDataTransformations <- function(table, columnDefinitions) {
transformations <- columnDefinitions[substr(columnDefinitions, 0, 1) == "#"]
transRes <- Reduce('cbind', lapply(transformations, MakeSingleDataTransformation, table = table))
cbind(table, transRes)
}
FilterAndOrderTable <- function(table, columnNames, columnsDefinitions) {
dataColumns <- names(columnsDefinitions[substr(columnsDefinitions, 0, 1) != "="])
columnNames <- columnNames[dataColumns]
columnNames <- unlist(columnNames[order(names(columnNames))])
table[, .SD, .SDcols = c(columnNames)]
}
TransformSingleTable <- function(table, conversionSetup) {
table <- MakeAllDataTransformations(table, conversionSetup$ColumnsDefinitions)
table <- FilterAndOrderTable(table, conversionSetup$ColumnNames, conversionSetup$ColumnsDefinitions)
table
}
TransformStudentData <- function(studentFiles, conversionSetup) {
lapply(studentFiles, TransformSingleTable, conversionSetup = conversionSetup)
}