-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDia1.R
219 lines (151 loc) · 5.8 KB
/
Dia1.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
library(tidyverse)
library(here) #refiere la ruta a la carpeta del proyecto
library(tidylog) #informa sobre operaciones dplyr y tidyr
#install.packages("summarytools")
library(summarytools) #resume de forma clara y rápida datos numéricos y categóricos
library(knitr) #reportar datos en varios formatos
#Datos
waste <- read_csv(here("Data/country_level_data.csv"))
glimpse(waste)
head(waste)
tail(waste)
# Ordenar variables
waste%>%arrange(population_population_number_of_people)
waste%>%arrange(desc(population_population_number_of_people)) # ordena de forma descendiente
# Cambiar nombre columnas:
waste%>% rename(population=population_population_number_of_people)
# Organizar columnas
waste %>% relocate(country_name, .before = iso3c)
# (Des)seleccionar variables:
waste %>% select(-region_id) # con menos no seleccionamos la variable
# Seleccionar sólo variables de interés:
waste_select <- waste %>%
select(iso3c,
region_id,
country = country_name, # cambiar el nombre directamente a la variable
income_id,
gdp,
population = population_population_number_of_people,
total_waste = total_msw_total_msw_generated_tons_year,
starts_with("composition")) %>% # selecciona todas las variables que empiezan por composition
arrange(desc(total_waste))
glimpse(waste_select)
# tabla de resumen de datos (libreria summarytools)
dfSummary(waste_select$region_id)
waste_select%>%
select(total_waste)%>%
dfSummary()
dfSummary(waste_select) # para todo el dataset
# Extraer valores únicos (niveles) de una(s) variable(s):
waste_select %>% distinct(income_id) # para variables categoricas
waste_select %>% distinct(region_id)
# Recodificar niveles de una variable:
waste_regions <- waste_select %>%
mutate(region_id = recode(region_id,
"EAS" = "East_Asia_Pacific",
"NAC" = "North_America",
"SAS" = "South_Asia",
"LCN" = "Latin_America",
"ECS" = "Europe_Central_Asia",
"SSF" = "Sub-Saharan_Africa",
"MEA" = "Middle_East_North_Africa"))
# Agrupar datos y resumir:
waste_regions %>%
group_by(region_id) %>%
summarise(total_waste = sum(total_waste, na.rm = TRUE))
# Crear nueva variable - Ej: transformar basura a millones de toneladas
waste_regions %>%
group_by(region_id) %>%
summarise(total_waste = sum(total_waste, na.rm = TRUE)) %>%
mutate(waste_mtons = total_waste/1000000)
# Filtrar datos:
waste_regions %>%
filter(region_id == "Latin_America")
waste_regions %>%
filter(region_id == "Europe_Central_Asia" & population <= 1000000)
# & = y, filtrar region id y population
# | = o, filtra regrion id o population
# ! = filtrame por todo lo que no sea
# para hacer cadena de datos %in% C()
#Crear nuevo factor:
# case_when crea una variable nueva creada en condicionales
waste_regions %>%
mutate(pop_size = case_when(
population >= 1000000 ~ "big",
population < 1000000 & population > 500000 ~ "medium",
population <= 500000 ~ "small")) %>%
relocate(pop_size, .before = population)
#+++++++++++++++++++++++++++++++++++++++++++++++++++++
### combinar base de datos con join ###
# datos
world_data <- read_csv2(here("data/world_data.csv"))
glimpse(world_data)
continent <- world_data %>%
select(iso_a3,
country_name = name_long,
continent)
glimpse(continent)
waste_world <- waste_regions %>%
rename(iso_a3 = iso3c) %>% # renombrar variable
full_join(continent, by = "iso_a3")
waste_world <- waste_regions %>%
rename(iso_a3 = iso3c) %>%
left_join(continent, by = "iso_a3")
# ¿Qué paises se han quedado sin identificar?
waste_world %>%
filter(is.na(continent)) %>%
pull(country, iso_a3) # pull funcion equivalente a $
#Buscar los paises que faltan en el dataset de continente:
continent %>%
filter(country_name %in%
c("Channel Islands",
"Gibraltar",
"Tuvalu",
"Kosovo",
"Taiwan"))
continent_corrected=continent %>%
mutate(iso_a3= ifelse(country_name== "Kosovo", "XKX", iso_a3),
iso_a3= ifelse(country_name== "Taiwan", "TWN ", iso_a3))
#Alternativa para buscar los paises que faltan en el dataset de continente:
#package stringr
continent %>%
filter(str_detect(country_name,"Kosovo|Gibraltar|Tuvalu|Channel Islands|Taiwan"))
#Corregir un dato:
continent_corrected <- continent %>%
mutate(iso_a3 = ifelse(country_name == "Kosovo","XKX", iso_a3)) %>%
mutate(iso_a3 = ifelse(country_name == "Taiwan","TWN", iso_a3))
#Volver a combinar dataset:
waste_world <- waste_regions %>%
rename(iso_a3 = iso3c) %>%
left_join(continent_corrected, by = "iso_a3")
#Guardar dataset para el próximo día:
write_csv(waste_world, here("Data/waste_world.csv"))
#++++++++++++++++++++++++++++++++++++++++++++++
### library tidyr ###
# Reestructurar el data set
# funcion pivot_wider() o spread()
# funcion pivot_longer() o gather()
composition <- waste_regions %>%
pivot_longer(cols = starts_with("composition"), names_to = "composition",
values_to = "percent")
glimpse(composition)
distinct(composition, composition)
# limpia los nombre dentro de una variable
composition <- composition %>%
mutate(composition=str_remove(composition,"composition_")) %>%
mutate(composition=str_remove(composition,"_percent"))
composition %>%
group_by(country) %>%
mutate(per_sum = sum(percent, na.rm = TRUE)) %>%
filter(per_sum >= 99.9) %>%
filter(per_sum <= 100.1)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#
# Git / GitHub #
library(usethis)
git_sitrep()
datos<- read_csv(here("Data/github_data.csv"))
plot(happiness~work.hours, data=datos)
#ggplot happines ~ work hours
ggplot(datos)+
geom_point(aes(work.hours, happiness))