-
Notifications
You must be signed in to change notification settings - Fork 10
/
as.quitte.R
254 lines (221 loc) · 8.98 KB
/
as.quitte.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
#' Class "quitte" ~~~
#'
#' The quitte class is a more standardized data.frame format. `is.quitte`
#' tests if `x` is an quitte-object, `as.quitte` transforms `x`
#' to an quitte-object (if possible).
#'
#'
#' @name quitte-class
#' @aliases quitte-class as.quitte as.quitte-methods
#' as.quitte,character-method
#' @docType class
#' @param x An object that should be either tested or transformed as/to an
#' quitte-object.
#' @param periodClass integer or POSIXct
#' @param addNA modifies a factor by turning NA into an extra level (so that NA
#' values are counted in tables, for instance).
#' @param na.rm if set to TRUE entries with value NA will be removed
#' @author Jan Philipp Dietrich
#' @keywords classes
#' @importFrom dplyr bind_rows filter relocate
#' @importFrom forcats fct_na_value_to_level
#' @importFrom magclass clean_magpie getNames getNames<- getSets getSets<-
#' @importFrom reshape2 melt
#' @importFrom rlang .data
#' @importFrom stats setNames
#' @importFrom tibble as_tibble
#'
#' @export
as.quitte <- function(x, periodClass = "integer", addNA = FALSE, na.rm = FALSE) { # nolint
UseMethod("as.quitte", x)
}
#' @export
as.quitte.character <- function(x, periodClass = "integer", addNA = FALSE, na.rm = FALSE) { # nolint
if (all(file.exists(x) & grepl("\\.(mif|csv|rds|xlsx?)$", x)))
return(as.quitte(read.quitte(x, sep = NULL),
periodClass = periodClass, addNA = addNA, na.rm = na.rm))
stop(
"Provided character cannot be converted to quitte as it does not seem to be a valid file path!"
)
}
#' @method as.quitte quitte
#' @export
as.quitte.quitte <- function(x, periodClass = "integer", addNA = FALSE, na.rm = FALSE) { # nolint
model <- scenario <- region <- variable <- unit <- period <- NULL
if (is.quitte(x, warn = FALSE)) {
if (addNA) x <- qaddNA(x)
if (na.rm) x <- x[!is.na(x$value), ]
return(relocate(x, model, scenario, region, variable, unit, period))
} else {
class(x) <- class(x)[class(x) != "quitte"]
return(as.quitte.data.frame(x, periodClass = periodClass, addNA = addNA, na.rm = na.rm))
}
}
#' @method as.quitte data.frame
#' @export
as.quitte.data.frame <- function(x, periodClass = "integer", addNA = FALSE, na.rm = FALSE) { # nolint
if (!(periodClass %in% c("integer", "POSIXct")))
stop("periodClass must be in c('integer', 'POSIXct')")
storeAttributes <- attributes(x)[-match(c("names", "row.names", "class"),
names(attributes(x)))]
mandatoryColumns <-
c("model",
"scenario",
"region",
"variable",
"unit",
"period",
"value")
factorColumns <- c("model", "scenario", "region", "variable", "unit")
colnames(x) <- tolower(colnames(x))
colnames(x)[colnames(x) == "year"] <- "period"
colnames(x)[colnames(x) == paste0("data", 1)] <- "scenario"
colnames(x)[colnames(x) == paste0("data", 2)] <- "model"
colnames(x)[colnames(x) == paste0("data", 3)] <- "variable"
if ( !"value" %in% colnames(x)
&& !all(is.na(suppressWarnings(as.integer(colnames(x)))))) {
x <- suppressMessages(melt(x))
colnames(x)[which(colnames(x) == "value") - 1] <- "period"
}
if (!all(mandatoryColumns %in% colnames(x))) {
if (!("model" %in% colnames(x)))
x <- cbind(x, model = fct_na_value_to_level(factor(NA),
level = '(Missing)'))
if (!("scenario" %in% colnames(x)))
x <- cbind(x, scenario = fct_na_value_to_level(factor(NA),
level = '(Missing)'))
if (!("region" %in% colnames(x)))
x <- cbind(x, region = as.factor("GLO"))
if (!("variable" %in% colnames(x)))
x <- cbind(x, variable = fct_na_value_to_level(factor(NA),
level = '(Missing)'))
if (!("unit" %in% colnames(x)))
x <- cbind(x, unit = fct_na_value_to_level(factor(NA),
level = '(Missing)'))
if (periodClass == "POSIXct" && !"period" %in% colnames(x))
x <- cbind(x, period = as.POSIXct(NA))
if (periodClass == "integer" && !"period" %in% colnames(x))
x <- cbind(x, period = NA_integer_)
if (!"value" %in% colnames(x))
stop("Data frame cannot be converted. A column \"value\" has to be provided!")
}
factorCheck <- sapply(x[, factorColumns], is.factor) # nolint
if (!all(factorCheck)) {
for (i in names(factorCheck)[!factorCheck])
x[[i]] <- as.factor(x[[i]])
}
if (is.factor(x$period)) {
x$period <- as.integer(as.character(x$period))
}
if (periodClass == "integer")
x$period <- as.integer(x$period)
if (periodClass == "POSIXct" && !("POSIXct" %in% attr(x$period, "class")))
x$period <- ISOyear(x$period)
if (!is.numeric(x$value))
stop("Value column must contain numeric data!")
# rearrange data for better readability
reorder <-
c(mandatoryColumns[mandatoryColumns != "value"], names(x)[!(names(x) %in% mandatoryColumns)], "value")
x <- x[reorder]
# add NA entrys to factors
if (addNA) x <- qaddNA(x)
if (na.rm) x <- x[!is.na(x$value), ]
attributes(x) <- c(attributes(x)[match(setdiff(names(attributes(x)),
names(storeAttributes)),
names(attributes(x)))],
storeAttributes)
class(x) <- c("quitte", class(x))
return(x)
}
#' @method as.quitte magpie
#' @export
as.quitte.magpie <- function(x, periodClass = "integer", addNA = FALSE, na.rm = FALSE) { # nolint
if (!(periodClass %in% c("integer", "POSIXct")))
stop("periodClass must be in c('integer', 'POSIXct')")
x <- clean_magpie(x, what = "sets")
if (getSets(x, fulldim = FALSE)[3] == "d3")
getSets(x, fulldim = FALSE)[3] <- "variable"
if ( !"unit" %in% getSets(x)
&& "variable" %in% getSets(x)
&& all(grepl(" \\(.*\\)$", getNames(x, fulldim = TRUE)$variable))
) {
getNames(x) <- sub(" \\(([^\\()]*)\\)($|\\.)", ".\\1\\2", getNames(x))
getSets(x, fulldim = FALSE)[3] <- sub("variable", "variable.unit",
getSets(x, fulldim = FALSE)[3])
}
d <- dimnames(x)
if (!is.null(names(d)[[3]])) {
datanames <- strsplit(names(d)[[3]], "\\.")[[1]]
datanames <- make.unique(c("cell", "region", "year", "value", datanames),
sep = "")[- (1:4)]
} else {
datanames <- NULL
}
x <- as.data.frame(x)
if (all(is.na(x$Cell)))
x$Cell <- NULL # nolint
if (length(datanames) > 0) {
for (i in seq_along(datanames))
colnames(x)[colnames(x) == paste0("Data", i)] <- datanames[i]
} else {
if ("Data1" %in% colnames(x) && all(levels(x$Data1) == "NA"))
x$Data1 <- NULL # nolint
}
quitteColumns <- c("model", "scenario", "region", "variable", "unit",
"period", "value")
for (cn in quitteColumns) {
colnames(x)[tolower(colnames(x)) == cn] <- cn
}
colnames(x)[tolower(colnames(x)) == "year"] <- "period"
if (!is.factor(x$region))
x$region <- fct_na_value_to_level(as.factor(x$region),
level = '(Missing)')
if (all(x$period == 0)) {
levels(x$period) <- NA
} else if (periodClass == "integer") {
x$period <- as.integer(as.character(x$period))
} else if (periodClass == "POSIXct") {
x$period <- ISOyear(x$period)
}
# add missing columns
missingColumns <- setdiff(quitteColumns, colnames(x))
if (length(missingColumns) > 0) {
x <- data.frame(
x,
setNames(
as.list(rep(fct_na_value_to_level(factor(NA), level = '(Missing)'),
length(missingColumns))),
missingColumns)
)
} else {
x <- data.frame(x)
}
# reorder columns
x <- x[c(match(quitteColumns, colnames(x)),
match(setdiff(colnames(x), quitteColumns), colnames(x)))]
# add NA entrys to factors
if (addNA)
x <- qaddNA(x)
if (na.rm)
x <- x[!is.na(x$value), ]
x <- as_tibble(x)
class(x) <- c("quitte", class(x))
return(x)
}
#' @method as.quitte list
#' @export
as.quitte.list <- function(x, periodClass = "integer", addNA = FALSE, na.rm = FALSE) { # nolint
return(bind_rows(lapply(x, as.quitte, periodClass = periodClass, addNA = addNA, na.rm = na.rm)))
}
#' @method as.quitte NULL
#' @export
as.quitte.NULL <- function(x, periodClass = "integer", addNA = FALSE, na.rm = FALSE) { # nolint
return(filter(as.quitte(as_tibble(as.quitte(data.frame(value = 0), periodClass = periodClass))), .data$value > 1))
}
qaddNA <- function(x) {
for (col in colnames(x)) {
if (is.factor(x[[col]]))
x[[col]] <- addNA(x[[col]], ifany = TRUE)
}
return(x)
}