rm(list = ls())
########
nothoudots<-function(x){
  y<-rep(0.,length(x))
  for (i in c(1:length(x))){
    if(nchar(x[i])==11) y[i]<-as.numeric(substr(as.character(x[i]),1,2))*1000000+as.numeric(substr(as.character(x[i]),4,6))*1000+as.numeric(substr(as.character(x[i]),8,10)) 
    if(nchar(x[i])==10) y[i]<-as.numeric(substr(as.character(x[i]),1,2))*1000000+as.numeric(substr(as.character(x[i]),4,6))*1000+as.numeric(substr(as.character(x[i]),8,10)) 
    if(nchar(x[i])==9) y[i]<-as.numeric(substr(as.character(x[i]),1,1))*1000000+as.numeric(substr(as.character(x[i]),3,5))*1000+as.numeric(substr(as.character(x[i]),7,9)) 
    if(nchar(x[i])==7) y[i]<-as.numeric(substr(as.character(x[i]),1,3))*1000+as.numeric(substr(as.character(x[i]),5,7)) 
    if(nchar(x[i])==6) y[i]<-as.numeric(substr(as.character(x[i]),1,2))*1000+as.numeric(substr(as.character(x[i]),4,6)) 
    if(nchar(x[i])==5) y[i]<-as.numeric(substr(as.character(x[i]),1,1))*1000+as.numeric(substr(as.character(x[i]),3,5)) 
    if(nchar(x[i])==3) y[i]<-as.numeric(as.character(x[i])) 
    if(nchar(x[i])==2) y[i]<-as.numeric(as.character(x[i])) 
    if(nchar(x[i])==1) y[i]<-as.numeric(as.character(x[i])) 
    if(nchar(x[i])==0) y[i]->is.na
  }
  return(y)
}
#
setwd("~/Dropbox/ND_TP_baseline2018")
#
########
# Total poblacion Espana por Edad, Sexo, Periodo  
# Poblacion residente por Sexo, Edad="TODAS", Provincias="TOTAL NACIONAL", Periodo 
# https://www.ine.es/jaxiT3/Tabla.htm?t=31304
# Principales series desde 1971
# Resultados por Provincias 
# Población residente por fecha, sexo y edad
#
# 86,87,..,>100 no existe hasta 1981 
# 100,101,..,>105 no existe hasta 2015 >> Se eliminan las categorias 
# 
datini<-read.csv("10256.csv",header=TRUE,sep=";",dec=",")
#
datini[,1]<-factor(datini[,1])
datini<-datini[!datini[,1]%in%levels(datini[,1])[c(4,6,7,8,9,10)],]
datini[,1]<-droplevels(datini[,1])
datini<-datini[!datini[,1]==levels(datini[,1])[c(87)],]
datini[,1]<-droplevels(datini[,1])
datini<-datini[!datini[,1]==levels(datini[,1])[c(102)],]
datini[,1]<-droplevels(datini[,1])
levels(datini[,1])
levels(datini[,1])[4]<-"-1"
levels(datini[,1])<-substr(levels(datini[,1]),1,2)
levels(datini[,1])
datini[,1]<-as.numeric(as.character(datini[,1]))
datini[datini[,1]<=-0.99,1]<-100
colnames(datini)[1]<-c("Edad")
#
datini[,2]<-factor(datini[,2])
levels(datini$Sexo)<-c("T","H","M")
#
datini[,3]<-as.numeric(datini[,3])+1970
datini[seq(2,length(datini[,2]),2)-1,3]<-datini[seq(2,length(datini[,2]),2)-1,3]-49+0.5
#
datini[,4]<-nothoudots(as.character(datini[,4]))
colnames(datini)[4]<-c("TP")
#
dat_TP_SEP<-cbind(datini[2],datini[1],datini[-c(1:2)])
summary(dat_TP_SEP)
#
save(dat_TP_SEP,file="10256.RData")
#
######
# Poblacion residente por Sexo, Edad="TODAS", Provincias="TOTAL NACIONAL", Periodo 
# https://www.ine.es/jaxiT3/Tabla.htm?t=31304
# Principales series desde 1971
# Resultados por Provincias 
# Población residente por fecha, sexo y edad
#
datini<-read.csv("31304bsc.csv",header=TRUE,sep=";")
#
datini<-datini[,-c(2:3)]
datini[,1]<-factor(datini[,1])
levels(datini$Sexo)<-c("T","H","M")
datini[,2]<-as.numeric(datini[,2])+1970
datini[seq(2,294,2)-1,2]<-datini[seq(2,294,2)-1,2]-49+0.5
datini[,3]<-nothoudots(as.character(datini[,3]))
#
plot(datini[,c(2,3)],col=datini[,1])
# 
dat_TP_SP<-datini
summary(dat_TP_SP)
#
save(dat_TP_SP,file="31304bsc.RData")
#
######
# Poblacion residente por Sexo, Edad, Provincias, Periodo
# https://www.ine.es/jaxiT3/Tabla.htm?t=31304
# Principales series desde 1971
# Resultados por Provincias 
# Población residente por fecha, sexo y edad
#
datini<-read.csv("31304.csv",header=TRUE,sep=";")
#
datini<-datini[,c(1,2,4,5,3)]
summary(datini)
#
datini[,1]<-factor(datini[,1])
levels(datini$Sexo)<-c("T","H","M")
#
datini<-datini[!datini[,2]=="Total",]
datini[,2]<-droplevels(datini[,2])
#
############### Pendiente: arreglar grupos de edad
#
datini[,4]<-nothoudots(as.character(datini[,4]))
# 
datini<-datini[!datini[,5]=="Total Nacional",]
datini[,5]<-droplevels(datini[,5])
#
datini[,3]<-as.numeric(datini[,3])+1970
datini[seq(2,294,2)-1,3]<-datini[seq(2,294,2)-1,3]-49+0.5
#
plot(datini[datini$Provincias=="08 Barcelona",c(2,4)],col=datini[,1])
#
dat_TP_SEPP<-datini
summary(dat_TP_SEPP)
#
#save(dat_TP_SEPP,file="31304.RData")
#