Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
robertopucp
GitHub Repository: robertopucp/1eco35_2022_2
Path: blob/main/Trabajo_grupal/WG2/Grupo_8_R.R
2714 views
1
rm(list=ls())
2
3
#Instalamos los paquetes para realizar las actividades
4
#install.packages("reshape")
5
# install.packages("dplyr")
6
# install.packages("readxl")
7
# install.packages("tidyr")
8
9
#Cargamos nuestras librerías
10
library(reshape)
11
library(dplyr)
12
library(tidyr)
13
library(readxl)
14
15
getwd()
16
user <- Sys.getenv("USERNAME") # username
17
print(user)
18
19
#Primero corremos el directorio e importamos la base de datos
20
setwd( paste0("C:/Users/",user,"/Documents/GitHub/1ECO35_2022_2/data"))
21
junin_data<- read_excel("Region_Junin.xlsx")
22
23
#1.-Obtenemos los nombres de mis variables
24
names(junin_data)
25
26
#2.1.-Detallamos nuestros tipos de variables
27
lapply(junin_data, class) #la mayoría de variables son "numeric", excepto region,district y Place
28
29
#2.2.-Mostramos los principales datos estadisticos: mediana, media, minimo, max, primer quintil,tercer quintil
30
summary(junin_data)
31
32
#3.-Verificamos que variables presentan missing values
33
34
#Al correr cada codigo, si resulta TRUE, significa que la variable entre corchetes presenta missing values.
35
#Si resulta FALSE, la variable no presenta missing values.
36
any(is.na(junin_data["Region"])) #No presenta missing values
37
any(is.na(junin_data["District"])) #No presenta Missings
38
any(is.na(junin_data["Place"])) #sí presenta missings
39
any(is.na(junin_data["4_6_years_men"])) #NO
40
any(is.na(junin_data["4_6_years_women"])) #NO
41
any(is.na(junin_data["4_6_years_total"])) #NO
42
any(is.na(junin_data["6_14_years_men"])) #SÍ
43
any(is.na(junin_data["6_14_years_women"])) #NO
44
any(is.na(junin_data["6_14_years_total"])) #NO
45
any(is.na(junin_data["man_read"])) #NO
46
any(is.na(junin_data["women_read"])) #SI
47
any(is.na(junin_data["total_read"])) #NO
48
any(is.na(junin_data["men_not_read"])) #NO
49
any(is.na(junin_data["women_not_read"])) #NO
50
any(is.na(junin_data["total_not_read"])) #SI
51
any(is.na(junin_data["man_write"]))
52
any(is.na(junin_data["women_write"]))
53
any(is.na(junin_data["total_write"]))
54
any(is.na(junin_data["men_not_write"]))
55
any(is.na(junin_data["women_not_write"])) #NO
56
any(is.na(junin_data["total_not_write"]))
57
any(is.na(junin_data["instruction_men"])) #NO
58
any(is.na(junin_data["instruction_women"]))
59
any(is.na(junin_data["instruction_total"]))
60
any(is.na(junin_data["no_instruction_men"]))
61
any(is.na(junin_data["no_instruction_women"]))
62
any(is.na(junin_data["no_instruction_total"]))
63
any(is.na(junin_data["finished_instr_men"]))
64
any(is.na(junin_data["finished_instr_women"]))
65
any(is.na(junin_data["finished_instr_total"]))
66
any(is.na(junin_data["not_finished_instr_men"]))
67
any(is.na(junin_data["not_finished_instr_women"]))
68
any(is.na(junin_data["not_finished_instr_total"]))
69
any(is.na(junin_data["peruvian_men"]))
70
any(is.na(junin_data["peruvian_women"]))
71
any(is.na(junin_data["foreign_men"]))
72
any(is.na(junin_data["foreign_women"]))
73
any(is.na(junin_data["whites"]))
74
any(is.na(junin_data["natives"]))
75
any(is.na(junin_data["mestizos"]))
76
any(is.na(junin_data["blacks"]))
77
78
#4.-Cambiamos nombres a las siguientes variables:
79
#comunidad en lugar de place, homxlee en lugar de men_not_read,
80
#mujerxlee en lugar de woman_not_read y totalxlee en lugar de total_not_read
81
82
junin_data= rename(junin_data, c(Place="Comunidad"))
83
junin_data= rename(junin_data, c(men_not_read="Homxlee"))
84
junin_data= rename(junin_data, c(women_not_read="Mujerxlee"))
85
junin_data= rename(junin_data, c(total_not_read="Totalxlee"))
86
87
#5.-Muestre los valores únicos de las siguientes variables ( comunidad , District)
88
89
unique(junin_data$Comunidad)
90
91
unique(junin_data$District)
92
93
#6.-Crear columnas con las siguiente información:
94
#el % de mujeres del que no escriben ni leen (mujerxlee/totalxlee)
95
96
#para hallar el % de las mujeres que no leen multiplicamos por 100 a la división (mujerxlee/totalxlee)
97
junin_data['%mujeresnoleen'] = 100 * junin_data['Mujerxlee'] / junin_data['Totalxlee']
98
#para hallar el % de las mujeres que no escriben multiplicamos por 100 a la división (mujerxlee/totalxlee)
99
junin_data['%mujeresnoescriben'] = 100 * junin_data['women_not_write'] / junin_data['total_not_write']
100
101
#% de varones que no escriben ni leen (homxlee/totalxlee)
102
#se realiza el mismo procedimiento que para mujeres
103
junin_data['%hombresnoleen'] = 100 * junin_data['Homxlee'] / junin_data['Totalxlee']
104
junin_data['%hombresnoescriben'] = 100 * junin_data['men_not_write'] / junin_data['total_not_write']
105
106
#% de nativos respecto al total de la población.
107
108
#primero creamos una variable "población total" que suma mujeres y hombres peruanos y extranjeros
109
junin_data['poblacióntotal']= junin_data['peruvian_women'] + junin_data['peruvian_men'] + junin_data['foreign_women'] + junin_data['foreign_men']
110
#hallamos el % de nativos respecto al total de la población
111
junin_data['%nativos'] = 100 * junin_data['natives'] / junin_data['poblacióntotal']
112
113
#7.-Crear una base de datos con la siguiente información:
114
115
#a. Quedarse con la información de los distritos de Ciudad del Cerro, Jauja, Acolla, San Gerónimo, Tarma, Oroya y Concepción
116
117
clean <- junin_data[junin_data$District %in% c("CIUDAD DEL CERRO","JAUJA","ACOLLA","SAN GERÓNIMO","TARMA","OROYA", "CONCEPCIÓN"),]
118
119
# b. Luego quedarse con las comunidades que cuentan con nativos y mestizos.
120
121
clean1 <- clean[ ! clean$natives %in% c("0"),] #nos quedamos con solo nativos.Primero eliminamos todas las filas que tengan 0 en nativos. No asumimos que los NA son igual a 0.
122
clean2 <- clean1[ ! clean1$mestizos %in% c("0"),] #nos quedamos con solo mestizos. Primeros eliminamos todas las filas que tengan 0 en mestizos. No asumimos que los NA son igual a 0.
123
124
# c. Solo quedarse con las variables trabajadas en el punto 6), nombre de distrito y comunidad.
125
Base_cleaned <- clean2[,c('District','Comunidad','%mujeresnoleen','%mujeresnoescriben','%hombresnoleen', '%hombresnoescriben', 'poblacióntotal','%nativos')] #seleccionar columnas solicitadas
126
View(Base_cleaned)
127
128
# d. Guardar la base de datos en formato csv en la carpeta data.
129
130
write.csv(Base_cleaned, '../data/Base_cleaned_WG(grupo8).csv')
131
132