rm(list=ls())
library(ineq)
setwd('D:\\areas_protegidas\\xls_dbf')

paises <- c("Argentina","Bolivia","Brazil","Chile","Colombia",
            "Ecuador","Guayana Francesa","Guyana","Peru",
            "Paraguay","Suriname","Uruguay","Venezuela","Guayanas", "All countries")

############################################################################################
# -1960

dat_1960<-read.table('todas_las_variables_sam_simp_fk_1000km_-1960.txt',sep='\t',header=T)

dat_1960$AREA_AL <-  rowSums(dat_1960[,21:33]) # creo el area para todo el continente
dat_1960$AP_AL <- rowSums(dat_1960[,35:47]) # creo el area protegido para todo el continente
dat_1960<-data.frame(dat_1960[,c(1:34,49,35:48,50)])  # muevo las columnas

datPAIS_1960 <-dat_1960[,c(21:35)] # 21:34
sumaPAIS_1960 <- colSums(datPAIS_1960[sapply(datPAIS_1960, is.numeric)], na.rm = TRUE)
sumaPAIS_1960 <- as.data.frame(sumaPAIS_1960)
sumaPAIS_1960$pais <- paises
class(sumaPAIS_1960)

datAP_1960<-dat_1960[,c(36:50)] # 35:48
sumaAP_1960 <- colSums(datAP_1960[sapply(datAP_1960, is.numeric)], na.rm = TRUE)
sumaAP_1960<- as.data.frame(sumaAP_1960)
sumaAP_1960$pais <- paises
class(sumaAP_1960)

AP_1960 <- cbind(sumaPAIS_1960,sumaAP_1960)
AP_1960$FP <- AP_1960$sumaAP_1960*100/AP_1960$sumaPAIS
AP_1960 <- AP_1960[,-(4)]

write.table(AP_1960, "salidas txt/FP_pais_-1960.txt",col.names=T, row.names=T)


############################################################################################
# -1970

dat_1970<-read.table('todas_las_variables_sam_simp_fk_1000km_-1970.txt',sep='\t',header=T)

dat_1970$AREA_AL <-  rowSums(dat_1970[,21:33]) # creo el area para todo el continente
dat_1970$AP_AL <- rowSums(dat_1970[,35:47]) # creo el area protegido para todo el continente
dat_1970<-data.frame(dat_1970[,c(1:34,49,35:48,50)])  # muevo las columnas

datPAIS_1970 <-dat_1970[,c(21:35)] # 21:34
sumaPAIS_1970 <- colSums(datPAIS_1970[sapply(datPAIS_1970, is.numeric)], na.rm = TRUE)
sumaPAIS_1970 <- as.data.frame(sumaPAIS_1970)
sumaPAIS_1970$pais <- paises
class(sumaPAIS_1970)

datAP_1970<-dat_1970[,c(36:50)] # 35:48
sumaAP_1970 <- colSums(datAP_1970[sapply(datAP_1970, is.numeric)], na.rm = TRUE)
sumaAP_1970<- as.data.frame(sumaAP_1970)
sumaAP_1970$pais <- paises
class(sumaAP_1970)

AP_1970 <- cbind(sumaPAIS_1970,sumaAP_1970)
AP_1970$FP <- AP_1970$sumaAP_1970*100/AP_1970$sumaPAIS
AP_1970 <- AP_1970[,-(4)]

write.table(AP_1970, "salidas txt/FP_pais_-1970.txt",col.names=T, row.names=T)


############################################################################################
# -1980

dat_1980<-read.table('todas_las_variables_sam_simp_fk_1000km_-1980.txt',sep='\t',header=T)

dat_1980$AREA_AL <-  rowSums(dat_1980[,21:33]) # creo el area para todo el continente
dat_1980$AP_AL <- rowSums(dat_1980[,35:47]) # creo el area protegido para todo el continente
dat_1980<-data.frame(dat_1980[,c(1:34,49,35:48,50)])  # muevo las columnas

datPAIS_1980 <-dat_1980[,c(21:35)] # 21:34
sumaPAIS_1980 <- colSums(datPAIS_1980[sapply(datPAIS_1980, is.numeric)], na.rm = TRUE)
sumaPAIS_1980 <- as.data.frame(sumaPAIS_1980)
sumaPAIS_1980$pais <- paises
class(sumaPAIS_1980)

datAP_1980<-dat_1980[,c(36:50)] # 35:48
sumaAP_1980 <- colSums(datAP_1980[sapply(datAP_1980, is.numeric)], na.rm = TRUE)
sumaAP_1980<- as.data.frame(sumaAP_1980)
sumaAP_1980$pais <- paises
class(sumaAP_1980)

AP_1980 <- cbind(sumaPAIS_1980,sumaAP_1980)
AP_1980$FP <- AP_1980$sumaAP_1980*100/AP_1980$sumaPAIS
AP_1980 <- AP_1980[,-(4)]

write.table(AP_1980, "salidas txt/FP_pais_-1980.txt",col.names=T, row.names=T)


############################################################################################
# -1990

dat_1990<-read.table('todas_las_variables_sam_simp_fk_1000km_-1990.txt',sep='\t',header=T)

dat_1990$AREA_AL <-  rowSums(dat_1990[,21:33]) # creo el area para todo el continente
dat_1990$AP_AL <- rowSums(dat_1990[,35:47]) # creo el area protegido para todo el continente
dat_1990<-data.frame(dat_1990[,c(1:34,49,35:48,50)])  # muevo las columnas

datPAIS_1990 <-dat_1990[,c(21:35)] # 21:34
sumaPAIS_1990 <- colSums(datPAIS_1990[sapply(datPAIS_1990, is.numeric)], na.rm = TRUE)
sumaPAIS_1990 <- as.data.frame(sumaPAIS_1990)
sumaPAIS_1990$pais <- paises
class(sumaPAIS_1990)

datAP_1990<-dat_1990[,c(36:50)] # 35:48
sumaAP_1990 <- colSums(datAP_1990[sapply(datAP_1990, is.numeric)], na.rm = TRUE)
sumaAP_1990<- as.data.frame(sumaAP_1990)
sumaAP_1990$pais <- paises
class(sumaAP_1990)

AP_1990 <- cbind(sumaPAIS_1990,sumaAP_1990)
AP_1990$FP <- AP_1990$sumaAP_1990*100/AP_1990$sumaPAIS
AP_1990 <- AP_1990[,-(4)]

write.table(AP_1990, "salidas txt/FP_pais_-1990.txt",col.names=T, row.names=T)


############################################################################################
# -2000

dat_2000<-read.table('todas_las_variables_sam_simp_fk_1000km_-2000.txt',sep='\t',header=T)

dat_2000$AREA_AL <-  rowSums(dat_2000[,21:33]) # creo el area para todo el continente
dat_2000$AP_AL <- rowSums(dat_2000[,35:47]) # creo el area protegido para todo el continente
dat_2000<-data.frame(dat_2000[,c(1:34,49,35:48,50)])  # muevo las columnas

datPAIS_2000 <-dat_2000[,c(21:35)] # 21:34
sumaPAIS_2000 <- colSums(datPAIS_2000[sapply(datPAIS_2000, is.numeric)], na.rm = TRUE)
sumaPAIS_2000 <- as.data.frame(sumaPAIS_2000)
sumaPAIS_2000$pais <- paises
class(sumaPAIS_2000)

datAP_2000<-dat_2000[,c(36:50)] # 35:48
sumaAP_2000 <- colSums(datAP_2000[sapply(datAP_2000, is.numeric)], na.rm = TRUE)
sumaAP_2000<- as.data.frame(sumaAP_2000)
sumaAP_2000$pais <- paises
class(sumaAP_2000)

AP_2000 <- cbind(sumaPAIS_2000,sumaAP_2000)
AP_2000$FP <- AP_2000$sumaAP_2000*100/AP_2000$sumaPAIS
AP_2000 <- AP_2000[,-(4)]

write.table(AP_2000, "salidas txt/FP_pais_-2000.txt",col.names=T, row.names=T)


############################################################################################
# -2010

dat_2010<-read.table('todas_las_variables_sam_simp_fk_1000km_-2010.txt',sep='\t',header=T)

dat_2010$AREA_AL <-  rowSums(dat_2010[,21:33]) # creo el area para todo el continente
dat_2010$AP_AL <- rowSums(dat_2010[,35:47]) # creo el area protegido para todo el continente
dat_2010<-data.frame(dat_2010[,c(1:34,49,35:48,50)])  # muevo las columnas

datPAIS_2010 <-dat_2010[,c(21:35)] # 21:34
sumaPAIS_2010 <- colSums(datPAIS_2010[sapply(datPAIS_2010, is.numeric)], na.rm = TRUE)
sumaPAIS_2010 <- as.data.frame(sumaPAIS_2010)
sumaPAIS_2010$pais <- paises
class(sumaPAIS_2010)

datAP_2010<-dat_2010[,c(36:50)] # 35:48
sumaAP_2010 <- colSums(datAP_2010[sapply(datAP_2010, is.numeric)], na.rm = TRUE)
sumaAP_2010<- as.data.frame(sumaAP_2010)
sumaAP_2010$pais <- paises
class(sumaAP_2010)

AP_2010 <- cbind(sumaPAIS_2010,sumaAP_2010)
AP_2010$FP <- AP_2010$sumaAP_2010*100/AP_2010$sumaPAIS
AP_2010 <- AP_2010[,-(4)]

write.table(AP_2010, "salidas txt/FP_pais_-2010.txt",col.names=T, row.names=T)


############################################################################################
# -2016

dat_2016<-read.table('todas_las_variables_sam_simp_fk_1000km.txt',sep='\t',header=T)

dat_2016$AREA_AL <-  rowSums(dat_2016[,21:33]) # creo el area para todo el continente
dat_2016$AP_AL <- rowSums(dat_2016[,35:47]) # creo el area protegido para todo el continente
dat_2016<-data.frame(dat_2016[,c(1:34,49,35:48,50)])  # muevo las columnas

datPAIS_2016 <-dat_2016[,c(21:35)] # 21:34
sumaPAIS_2016 <- colSums(datPAIS_2016[sapply(datPAIS_2016, is.numeric)], na.rm = TRUE)
sumaPAIS_2016 <- as.data.frame(sumaPAIS_2016)
sumaPAIS_2016$pais <- paises
class(sumaPAIS_2016)

datAP_2016<-dat_2016[,c(36:50)] # 35:48
sumaAP_2016 <- colSums(datAP_2016[sapply(datAP_2016, is.numeric)], na.rm = TRUE)
sumaAP_2016<- as.data.frame(sumaAP_2016)
sumaAP_2016$pais <- paises
class(sumaAP_2016)

AP_2016 <- cbind(sumaPAIS_2016,sumaAP_2016)
AP_2016$FP <- AP_2016$sumaAP_2016*100/AP_2016$sumaPAIS
AP_2016 <- AP_2016[,-(4)]

write.table(AP_2016, "salidas txt/FP_pais_-2016.txt",col.names=T, row.names=T)