Cargamos e instalamos las librerias correspondientes
library(tables)
## Loading required package: Hmisc
## Loading required package: lattice
## Loading required package: survival
## Loading required package: Formula
## Loading required package: ggplot2
##
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:base':
##
## format.pval, round.POSIXt, trunc.POSIXt, units
library(knitr)
## Warning: package 'knitr' was built under R version 3.3.3
Leemos el fichero en cuestión y lo asigno a df.
df<- read.table("paMunMur0015DF.csv",sep=";",header=TRUE)
Con las funciones pertinentes, comprobamos la estructura y lo que representan las variables
head(df)
## codMun Mun year pobTotal pobHom pobMuj
## 1 1 Abanilla 2000 6106 3050 3056
## 2 1 Abanilla 2001 6033 3030 3003
## 3 1 Abanilla 2002 6040 3045 2995
## 4 1 Abanilla 2003 6239 3151 3088
## 5 1 Abanilla 2004 6145 3084 3061
## 6 1 Abanilla 2005 6265 3171 3094
str(df)
## 'data.frame': 720 obs. of 6 variables:
## $ codMun : int 1 1 1 1 1 1 1 1 1 1 ...
## $ Mun : Factor w/ 45 levels "Ãguilas","Abanilla",..: 2 2 2 2 2 2 2 2 2 2 ...
## $ year : int 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 ...
## $ pobTotal: int 6106 6033 6040 6239 6145 6265 6333 6568 6642 6589 ...
## $ pobHom : int 3050 3030 3045 3151 3084 3171 3212 3350 3390 3315 ...
## $ pobMuj : int 3056 3003 2995 3088 3061 3094 3121 3218 3252 3274 ...
Seleccionamos los datos correspondientes al año 2000 y los guardamos en el objeto que nos indican
a2000 <- df [df$year=="2000", ]
Calculamos la población de Murcia para ese año
table(a2000$pobTotal,a2000$year)
##
## 2000
## 593 1
## 959 1
## 997 1
## 1369 1
## 1568 1
## 1582 1
## 2035 1
## 3409 1
## 3908 1
## 5562 1
## 5749 1
## 6106 1
## 6814 1
## 7052 1
## 7305 1
## 7513 1
## 8116 1
## 8460 1
## 8888 1
## 10140 1
## 10753 1
## 10859 1
## 10972 1
## 12241 1
## 13738 1
## 14171 1
## 14516 1
## 14606 1
## 15583 1
## 15856 1
## 16003 1
## 18847 1
## 18925 1
## 20854 1
## 22250 1
## 22359 1
## 22719 1
## 27559 1
## 29319 1
## 32126 1
## 33453 1
## 44389 1
## 72000 1
## 179939 1
## 357166 1
# salvamos el "problema" del numero 1 con la función "aggregate" y "list"
aggregate( a2000$pobTotal, list( anho = a2000$year ),
sum, simplify = T )
## anho x
## 1 2000 1149328
Ahora hacemos todo lo referente a la funcion “tabular” para la poblacion masculina en el año 2015. Además nombramos propiamente a la columna que lo necesita.
a2015 <- df [df$year=="2015", ]
tt<-tabular( ~ pobHom*( (media=mean) + sd + median + max + min ), data = a2015 )
html( tt,
options = htmloptions( HTMLcaption = "Hombres en 2015" ),
pad = TRUE)
Hombres en 2015
|
|
pobHom
|
|
|
media
|
sd
|
median
|
max
|
min
|
|
|
16343
|
34857
|
7954
|
215177
|
270
|
Realizamos el gráfico como el del enunciado, introduciéndo en código pertinente
plot(muni$year,muni$pobTotal,type = "l",
xlab = "Año",
ylab= "Número de habitantes",
main = "Archena",
ylim = c( 0, max( muni$pobTotal )),
lwd=3
)
lines(muni$year,muni$pobHom,lwd=1,col="blue")
lines(muni$year,muni$pobMuj,lty=2,col="blue")
legend( "bottomright",
cex = 0.7,
legend=c("Total","Mujeres","Hombres"),
col = c("black", "blue", "blue"),
title = "Poblaciones",
lwd = 2:1,
lty = 1:2)
