Procesamiento de Cenagro 2012

Lectura de databases

setwd("C:/Users/sant_/Documents/Consultorias/2019/nico/cenagro")
library(foreign)
##Bases generales
### Ancash
ancash_rec02 <- read.dbf("ancash/338-Modulo231/03_IVCENAGRO_REC02.dbf") 
ancash_rec04a <- read.dbf("ancash/338-Modulo236/08_IVCENAGRO_REC04A.dbf") 
### Cajamarca
cajamarca_rec02 <- read.dbf("cajamarca/342-Modulo231/03_IVCENAGRO_REC02.dbf") 
cajamarca_rec04a <- read.dbf("cajamarca/342-Modulo236/08_IVCENAGRO_REC04A.dbf") 
### Ica
ica_rec02 <- read.dbf("ica/347-Modulo231/03_IVCENAGRO_REC02.dbf") 
ica_rec04a <- read.dbf("ica/347-Modulo236/08_IVCENAGRO_REC04A.dbf") 
### Junín
junin_rec02 <- read.dbf("junin/348-Modulo231/03_IVCENAGRO_REC02.dbf") 
junin_rec04a <- read.dbf("junin/348-Modulo236/08_IVCENAGRO_REC04A.dbf") 
### La Libertad
lalibertad_rec02 <- read.dbf("lalibertad/349-Modulo231/03_IVCENAGRO_REC02.dbf") 
lalibertad_rec04a <- read.dbf("lalibertad/349-Modulo236/08_IVCENAGRO_REC04A.dbf")
### Lima
lima_rec02 <- read.dbf("lima/351-Modulo231/03_IVCENAGRO_REC02.dbf") 
lima_rec04a <- read.dbf("lima/351-Modulo236/08_IVCENAGRO_REC04A.dbf") 

Unión de bases

ancash_rec02$dpto <- "Ancash"
ancash_rec04a$dpto <- "Ancash" 
cajamarca_rec02$dpto <- "Cajamarca" 
cajamarca_rec04a$dpto <- "Cajamarca"
ica_rec02$dpto <- "Ica"
ica_rec04a$dpto <- "Ica"
junin_rec02$dpto <- "Junín"
junin_rec04a$dpto <- "Junín"
lalibertad_rec02$dpto <- "La Libertad"
lalibertad_rec04a$dpto <- "La Libertad"
lima_rec02$dpto <- "Lima"
lima_rec04a$dpto <- "Lima"

rec02 <- rbind(ancash_rec02,cajamarca_rec02)
rec02 <- rbind(rec02,ica_rec02)
rec02 <- rbind(rec02,junin_rec02)
rec02 <- rbind(rec02,lalibertad_rec02)
rec02 <- rbind(rec02,lima_rec02)

rec04a <- rbind(ancash_rec04a,cajamarca_rec04a)
rec04a <- rbind(rec04a,ica_rec04a)
rec04a <- rbind(rec04a,junin_rec04a)
rec04a <- rbind(rec04a,lalibertad_rec04a)
rec04a <- rbind(rec04a,lima_rec04a)

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
rec02 <- mutate(rec02, ubigeo=paste(rec02$P001,rec02$P002,rec02$P003),
                ccpp=paste(rec02$P001,rec02$P002,rec02$P003,rec02$P008))
rec02$ubigeo <- gsub(" ","",rec02$ubigeo)
rec02$ccpp <- gsub(" ","",rec02$ccpp)

rec04a <- mutate(rec04a, ubigeo=paste(rec04a$P001,rec04a$P002,rec04a$P003),
                ccpp=paste(rec04a$P001,rec04a$P002,rec04a$P003,rec04a$P008))
rec04a$ubigeo <- gsub(" ","",rec04a$ubigeo)
rec04a$ccpp <- gsub(" ","",rec04a$ccpp)

rec02 <- subset(rec02, select=c(dpto,ubigeo,ccpp,NPRIN,P024_03,P025))
rec04a <- subset(rec04a, select=c(dpto,ubigeo,ccpp,NPRIN,P067_01,P067_03))

## Papas y cítricos
rec02$producto[rec02$P024_03==2610 | rec02$P024_03==2611 | rec02$P024_03==2612 | rec02$P024_03==2613 | rec02$P024_03==2614 | rec02$P024_03==2615 | rec02$P024_03==5049 | rec02$P024_03==5062 | rec02$P024_03==5074 | rec02$P024_03==5075 | rec02$P024_03==5076 | rec02$P024_03==5077 | rec02$P024_03==5078 | rec02$P024_03==5079 | rec02$P024_03==5080 | 
rec02$P024_03==5081 | rec02$P024_03==5090 | rec02$P024_03==5091 | rec02$P024_03==5113 | rec02$P024_03==5122 | rec02$P024_03==5123 | rec02$P024_03==5124 | rec02$P024_03==5125 | rec02$P024_03==5126 | rec02$P024_03==5130 | rec02$P024_03==5135 | rec02$P024_03==5136 | rec02$P024_03==5137 | rec02$P024_03==5142 | rec02$P024_03==5145 | 
rec02$P024_03==5146 | rec02$P024_03==5155 | rec02$P024_03==5157 | rec02$P024_03==5159 | rec02$P024_03==5160 | rec02$P024_03==5170 | rec02$P024_03==5183 | rec02$P024_03==5184 | rec02$P024_03==5185 | rec02$P024_03==5213 | rec02$P024_03==5214 | rec02$P024_03==5231 | rec02$P024_03==5304 | rec02$P024_03==5344 | rec02$P024_03==5345 | 
rec02$P024_03==5346 | rec02$P024_03==5347 | rec02$P024_03==5395 | rec02$P024_03==5399 | rec02$P024_03==5406 | rec02$P024_03==5412 | rec02$P024_03==5436 | rec02$P024_03==5440 | rec02$P024_03==5441 | rec02$P024_03==5442 | rec02$P024_03==5458 | rec02$P024_03==5496 | rec02$P024_03==5512 | rec02$P024_03==5554 | rec02$P024_03==5586 | 
rec02$P024_03==5651 | rec02$P024_03==5658 | rec02$P024_03==5689 | rec02$P024_03==5733 | rec02$P024_03==5819 | rec02$P024_03==5942 | rec02$P024_03==5952 | rec02$P024_03==6099 | rec02$P024_03==6147 | rec02$P024_03==6148 | rec02$P024_03==6149 | rec02$P024_03==6482 | rec02$P024_03==6811 | rec02$P024_03==6898 | rec02$P024_03==6940 | 
rec02$P024_03==7125 | rec02$P024_03==7126 | rec02$P024_03==7127 | rec02$P024_03==7219 | rec02$P024_03==8000 | rec02$P024_03==8001 | rec02$P024_03==8002 | rec02$P024_03==8017 | rec02$P024_03==8074 | rec02$P024_03==8086 | rec02$P024_03==8163 | rec02$P024_03==8194 | rec02$P024_03==8207 | rec02$P024_03==8217 | rec02$P024_03==8218 | 
rec02$P024_03==8219 | rec02$P024_03==8233 | rec02$P024_03==8253 | rec02$P024_03==8275 | rec02$P024_03==8276 | rec02$P024_03==8286 | rec02$P024_03==8287 | rec02$P024_03==8320 | rec02$P024_03==8404 | rec02$P024_03==8408] <- 2 

## Naranjas y mandarinas
rec02$producto[rec02$P024_03==1146 | rec02$P024_03==5533 | rec02$P024_03==5568 | rec02$P024_03==5593 | rec02$P024_03==5611 | rec02$P024_03==5641 | rec02$P024_03==5751 | rec02$P024_03==5754 | rec02$P024_03==5762 | rec02$P024_03==5765 | rec02$P024_03==5810 | rec02$P024_03==5929 | rec02$P024_03==5931 | rec02$P024_03==6004 | rec02$P024_03==6045 | 
rec02$P024_03==6066 | rec02$P024_03==6087 | rec02$P024_03==6088 | rec02$P024_03==6089 | rec02$P024_03==6096 | rec02$P024_03==6097 | rec02$P024_03==6098 | rec02$P024_03==6099 | rec02$P024_03==6100 | rec02$P024_03==6101 | rec02$P024_03==6102 | rec02$P024_03==6103 | rec02$P024_03==6121 | rec02$P024_03==6190 | rec02$P024_03==6216 | 
rec02$P024_03==6312 | rec02$P024_03==6313 | rec02$P024_03==6378 | rec02$P024_03==6405 | rec02$P024_03==6419 | rec02$P024_03==6420 | rec02$P024_03==6421 | rec02$P024_03==6422 | rec02$P024_03==6423 | rec02$P024_03==6424 | rec02$P024_03==6470 | rec02$P024_03==6517 | rec02$P024_03==6539 | rec02$P024_03==6660 | rec02$P024_03==6991 | 
rec02$P024_03==6992 | rec02$P024_03==7192 | rec02$P024_03==6650 | rec02$P024_03==6969 | rec02$P024_03==8384] <- 3 

rec02 <- subset(rec02, rec02$producto==2 | rec02$producto==3)

## Vacas
rec04a$producto[rec04a$P067_01==674] <- 1

## Pollos
rec04a$producto[rec04a$P067_01==761] <- 4

rec04a <- subset(rec04a, rec04a$producto==1 | rec04a$producto==4)

## Renombrando variables
rec02 <- rename(rec02,cod_producto=P024_03, produccion=P025, ID=NPRIN)
rec04a <- rename(rec04a,cod_producto=P067_01, produccion=P067_03, ID=NPRIN)

base_cenagro <- rbind(rec02,rec04a)
base_cenagro$producto <- factor(base_cenagro$producto,
                                levels = c(1,2,3,4),
                                labels = c("Ganado vacuno","Papas","Cítricos","Pollos") )

Exportando etiquetas de distritos y CCPP

setwd("C:/Users/sant_/Documents/Consultorias/2019/nico/cenagro")

library(readxl)
ccpp <- read_excel("CENAGRO_2012.xlsx")
## readxl works best with a newer version of the tibble package.
## You currently have tibble v1.4.2.
## Falling back to column name repair from tibble <= v1.4.2.
## Message displays once per session.
ccpp <- subset(ccpp, select = c(IDCCPP,NOMBCCPP))

library(foreign)
distritos <- read.csv("equivalencia-ubigeos-oti-concytec.csv", sep=",")
distritos <- subset(distritos, select = c(cod_ubigeo_inei,desc_ubigeo_inei))

Importando etiquetas de distritos y ccpp

base_cenagro <- merge.data.frame(base_cenagro,ccpp,by.x = "ccpp", by.y = "IDCCPP", all.x = TRUE, all.y = FALSE)

base_cenagro <- merge.data.frame(base_cenagro,distritos,by.x = "ubigeo", by.y = "cod_ubigeo_inei", all.x = TRUE, all.y = FALSE)

base_cenagro <- rename(base_cenagro, Centro_Poblado=NOMBCCPP, Distrito=desc_ubigeo_inei)

base_cenagro <- select(base_cenagro, c(ID, ubigeo, ccpp, dpto, Distrito, Centro_Poblado,producto,produccion))

Reporte base de datos por productor

#Bases de datos por productor
base_cenagro_ica <- subset(base_cenagro, dpto=="Ica")

library(DT)
datatable(base_cenagro_ica, colnames = c("ID","Ubigeo","CodCCPP","Dpto","Distrito","CCPP","Producto","Producción"), extensions = 'Buttons', options = list(pageLength = 25, dom='Bfrtip', buttons=c('copy','csv','pdf')), filter = 'top', caption = "Base de datos de productores de ganado vacuno, papas, cítricos y pollos para 6 departamentos")
## Warning in instance$preRenderHook(instance): It seems your data is too
## big for client-side DataTables. You may consider server-side processing:
## https://rstudio.github.io/DT/server.html