Created
May 24, 2018 16:21
-
-
Save geofis/1dbac27f612eef8a40ed599d4181ac1c to your computer and use it in GitHub Desktop.
Leer y transformar datos de ONAMET
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(readxl) | |
| library(stringr) | |
| library(reshape2) | |
| library(tidyr) | |
| library(dplyr) | |
| library(lubridate) | |
| #FUNCION EXTRAER NUMEROS | |
| numextract <- function(string){ | |
| str_extract(string, "\\-*\\d+\\.*\\d*") | |
| } | |
| #DATOS ONAMET | |
| setwd(tempdir()) #FIJAR UN DIRECTORIO DE TRABAJO TEMPORAL | |
| download.file("http://geografiafisica.org/r/onamet/tstonamet.xlsx", 'tstonamet.xlsx') #DESCARGA EL ARCHIVO ZIP Y LO NOMBRA COMO TEMPORAL | |
| setwd(directorio) | |
| d <- read_xlsx("tstonamet.xlsx", sheet = 1, col_names = F) | |
| d <- as.data.frame(d) | |
| #ANOS, MUCHOS ANOS | |
| anos <- sort(numextract(melt(sapply(d, function(x) grep('DATOS DIARIOS', x, value = T)))$value)) | |
| #RANGOS DE DATOS | |
| Sys.setlocale(locale="en_GB.UTF-8") | |
| inicioano <- grep('^DIA.*$', d[,1]) | |
| finano <- grep('^TOTAL$', d[,1]) | |
| rdat <- data.frame(anos, inicioano, finano) | |
| seqdias <- seq.Date(as.Date(paste0(rdat$anos[1],'/1/1')), as.Date(paste0(rdat$anos[nrow(rdat)],'/12/31')), 'days') | |
| seqtable <- data.frame(year=year(seqdias), month=months(seqdias), day=day(seqdias)) | |
| seqtable <- as.data.frame(sapply(seqtable, as.character), stringsAsFactors = F) | |
| datos <- sapply(levels(rdat$anos), function(x) d[(rdat[rdat$anos==x,'inicioano']+1):(rdat[rdat$anos==x,'finano']-1), 1:13], simplify = F) | |
| datos <- melt(datos) | |
| colnames(datos) <- c('day',month.name, 'year') | |
| datos | |
| datos <- datos %>% gather(month, value, -day, -year) | |
| datoscalendario <- merge(seqtable, datos, all.x=T, sort = F) | |
| datoscalendario$value <- gsub('INAP','0', datoscalendario$value) | |
| datoscalendario$value <- gsub('-','NA', datoscalendario$value) | |
| datoscalendario$value <- as.numeric(datoscalendario$value) | |
| datoscalendario$fecha <- as.Date(with(datoscalendario, paste0(day,'/',month,'/', year)), format = '%d/%B/%Y') | |
| datoscalendario | |
| View(datoscalendario) | |
| write.csv(datoscalendario, 'datoscalendario.csv') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment