R/BBVAReportes.R

library(dplyr)
library(odbc)
library(DBI)
library(tidyverse)
library(lubridate)
library(ggplot2)
library(reshape2)

PasaListaAMatizISAT <- function (dfLista, cIDRenglon, cIDColumna, cIDContenido) {
  # Pasa de una lista a una matriz como en este ejemplo: (version para ISAT)
  #   A1 a1
  #   A2 a2           1  2  3
  #   A3 a3        A a1 a2 a3
  #   B1 b1   ---> B b1 b2
  #   B2 b2        C c1 c2 c3
  #   C1 c1
  #   C2 c2
  #   C3 c3
  #
  # Insumos:
  #   dfLista      : que es el argumento x
  #   cIDRenglon   : que es el argumento x
  #   cIDColumna   : que es el argumento x
  #   cIDContenido : que es el argumento x
  #
  # Resultado:
  #   dfMatriz : la lista en forma de matriz

  cNombreRenglones <- c('00:00', '00:30', '01:00', '01:30', '02:00', '02:30',
                        '03:00', '03:30','04:00', '04:30', '05:00', '05:30',
                        '06:00', '06:30','07:00', '07:30', '08:00', '08:30',
                        '09:00', '09:30','10:00', '10:30', '11:00', '11:30',
                        '12:00', '12:30','13:00', '13:30', '14:00', '14:30',
                        '15:00', '15:30','16:00', '16:30', '17:00', '17:30',
                        '18:00', '18:30','19:00', '19:30', '20:00', '20:30',
                        '21:00', '21:30','22:00', '22:30', '23:00', '23:30')
  cNombreColumnas <- unique(dfLista[, cIDColumna])[[1]]
  m <- length(cNombreRenglones)
  n <- length(cNombreColumnas)

  dfMatriz <- matrix(rep(0, m * n), m, n)
  dfMatriz <- as.data.frame(dfMatriz)
  rownames(dfMatriz) <- cNombreRenglones
  colnames(dfMatriz) <- cNombreColumnas

  for (i in 1:m) {
    k <- which(dfLista[, cIDRenglon] == cNombreRenglones[i])
    dfLista2 <- dfLista[k, ]
    if (length(k) > 0) {
      for (j in 1:n) {
        kk <- which(dfLista[k, cIDColumna] == cNombreColumnas[j])
        if(length(kk) > 0) {
          try(dfMatriz[i, j] <- dfLista2[kk, cIDContenido])
        }
      }
    }
  }

  return(dfMatriz)

}

con <- dbConnect(odbc(),
                 dsn = "BBVAReportes",
                 UID = "developer001",
                 PWD = rstudioapi::askForPassword("Database password"))
dbListTables(con)
rpRuteo <- dbReadTable(con, "RP_Ruteo")

rpRuteo %>%
  ggplot(aes(Hora_VDN_Linked)) + geom_freqpoly(binwidth = 1800)

fecha <- ymd(20180327)
longVentana <- dminutes(30) - 1

k <- which(date(rpRuteo$Hora_VDN_Linked) == fecha)
rpRuteo30mins <- rpRuteo[k, ] %>%
  mutate(hour_of_day = hour(as.POSIXct(strptime(Hora_VDN_Linked,
                                                "%Y-%m-%d %H:%M:%S"))),
         half_hour = (minute(as.POSIXct(strptime(Hora_VDN_Linked,
                                                 "%Y-%m-%d %H:%M:%S"))) < 31) * 30,
         time = paste0(formatC(hour_of_day, 2, width = 2, flag="0"),
                       ":", formatC(half_hour, 2, width = 2, flag="0"))) %>%
  group_by(time, Criterio_Aplicado) %>%
  summarise(conteo = sum(Rep_Key!=";"))

rpRuteo30mins$Criterio_Aplicado <- as.character(rpRuteo30mins$Criterio_Aplicado)
rpRuteoMatriz <- PasaListaAMatizISAT(rpRuteo30mins, 'time', 'Criterio_Aplicado', 'conteo')

rpRuteoMatriz$time <- rownames(rpRuteoMatriz)
rpRuteoMatriz %>%
  melt(id.vars = 'time') %>%
  ggplot(aes(x=time, y=value,colour=variable,group=variable)) + geom_line()
msanavarro/bayesiana documentation built on May 25, 2019, 8:32 p.m.