Last active
May 23, 2020 13:42
-
-
Save robintux/580d21774753c0d9cb04cdbe3ac8772e to your computer and use it in GitHub Desktop.
Script para crear vectores de la edad, distrito, provincia y distrito. con el objetivo de hacer estadistica descriptiva
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ============================================================================= | |
# Abraham Zamudio [GMMNS] | |
# 2020/05/22 | |
# Lima-Peru | |
# ============================================================================= | |
# https://www.datosabiertos.gob.pe/dataset/fallecidos-por-covid-19-ministerio-de-salud-minsa | |
muertos <- read.csv("FALLECIDOS_CDC.csv") | |
# Transformamos las variables FECHA a datos de tipo Date : | |
muertos$FECHA_NAC <- as.Date(muertos$FECHA_NAC, "%d/%m/%Y") | |
muertos$FECHA_FALLECIMIENTO <- as.Date(muertos$FECHA_FALLECIMIENTO, "%d/%m/%Y") | |
# transformamos las variables Departamnto, Provincia y Distrito a variables categoricas | |
muertos$DEPARTAMENTO <- as.factor(muertos$DEPARTAMENTO) | |
muertos$PROVINCIA <- as.factor(muertos$PROVINCIA) | |
muertos$DISTRITO <- as.factor(muertos$DISTRITO) | |
levels(muertos$DEPARTAMENTO) | |
levels(muertos$PROVINCIA) | |
levels(muertos$DISTRITO) | |
# Existen elementos NULL, hay que cambiarlos por NA | |
muertos$PROVINCIA[muertos$PROVINCIA == 'NULL'] <- NA | |
muertos$DISTRITO[muertos$DISTRITO == 'NULL'] <- NA | |
muertos$PROVINCIA[muertos$PROVINCIA == ''] <- NA | |
muertos$DISTRITO[muertos$DISTRITO == ''] <- NA | |
# Eliminamos los NA | |
muertos <- na.omit(muertos) | |
# Funcion age : calcula la edad entre la fecha de fallecimiento y la fecha de | |
# nacimiento | |
library(lubridate) | |
age <- function(dob, age.day = today(), units = "years", floor = TRUE) { | |
calc.age = interval(dob, age.day) / duration(num = 1, units = units) | |
if (floor) return(as.integer(floor(calc.age))) | |
return(calc.age) | |
} | |
# agregamos una columna edad al dataframe muertos | |
muertos <- transform(muertos , edad = age(FECHA_NAC, FECHA_FALLECIMIENTO)) | |
# Sacamos una muestra aleatoria | |
muestra <- muertos[sample(nrow(muertos),100), ] | |
write.table(matrix(muestra$edad, ncol = nrow(muestra)), | |
"edad.txt", | |
row.names = FALSE, | |
col.names = FALSE, | |
sep = ",") | |
write.table(matrix(muestra$DEPARTAMENTO, ncol = nrow(muestra)), | |
"DEPARTAMENTO.txt", | |
row.names = FALSE, | |
col.names = FALSE, | |
sep = ",") | |
write.table(matrix(muestra$PROVINCIA, ncol = nrow(muestra)), | |
"PROVINCIA.txt", | |
row.names = FALSE, | |
col.names = FALSE, | |
sep = ",") | |
write.table(matrix(muestra$DISTRITO, ncol = nrow(muestra)), | |
"DISTRITO.txt", | |
row.names = FALSE, | |
col.names = FALSE, | |
sep = ",") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment