-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCore.R
More file actions
28 lines (21 loc) · 752 Bytes
/
Core.R
File metadata and controls
28 lines (21 loc) · 752 Bytes
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
path = 'http://www.cores.es/sites/default/files/archivos/estadisticas/consumos-pp.xlsx'
get_core <- function(path, hoja){
require(zoo)
require(lubridate)
require(data.table)
require(readxl)
temp <- tempfile()
download.file(path, temp, mode = 'wb')
data <- data.table(read_xlsx(temp, sheet = hoja))
m = which(data[[1]]=='Año')
dt = data[!1:m]
colnames(dt) = as.character(unlist(data[m,]))
dt <- dt[!is.na(dt[[1]]) & dt[[2]] != 'total']
dt$Año <- as.yearmon(dym(paste0(dt[[1]],'-', dt[[2]])))
dt$Mes = NULL
dt = as.data.table(dt)
dt[, names(dt)[-1] := lapply(.SD, function(x){as.numeric(as.character(x))}),
.SD = names(dt)[-1]]
return(dt)
}
dt <- get_core(path, 'Gasolinas')