library (psych)
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
library( tidyr )
## Warning: package 'tidyr' was built under R version 3.4.4
library(graphics)

lectura de los datos

df <- read.csv("pobMur15.csv", header = T, sep = ";")
head(df)
##   codMun      Mun year pobHom pobMuj
## 1      1 Abanilla 2000   3050   3056
## 2      1 Abanilla 2001   3030   3003
## 3      1 Abanilla 2002   3045   2995
## 4      1 Abanilla 2003   3151   3088
## 5      1 Abanilla 2004   3084   3061
## 6      1 Abanilla 2005   3171   3094
summary(df)
##      codMun                 Mun           year          pobHom      
##  Min.   :  1.00   Águilas    : 16   Min.   :2000   Min.   :   270  
##  1st Qu.: 12.00   Abanilla    : 16   1st Qu.:2004   1st Qu.:  3348  
##  Median : 23.00   Abarán     : 16   Median :2008   Median :  7324  
##  Mean   : 61.09   Albudeite   : 16   Mean   :2008   Mean   : 15338  
##  3rd Qu.: 34.00   Alcantarilla: 16   3rd Qu.:2011   3rd Qu.: 13535  
##  Max.   :902.00   Aledo       : 16   Max.   :2015   Max.   :218501  
##                   (Other)     :624                                  
##      pobMuj      
##  Min.   :   233  
##  1st Qu.:  3255  
##  Median :  7158  
##  Mean   : 15099  
##  3rd Qu.: 12750  
##  Max.   :224712  
## 
str (df)
## 'data.frame':    720 obs. of  5 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 ...
##  $ pobHom: int  3050 3030 3045 3151 3084 3171 3212 3350 3390 3315 ...
##  $ pobMuj: int  3056 3003 2995 3088 3061 3094 3121 3218 3252 3274 ...
names (df)
## [1] "codMun" "Mun"    "year"   "pobHom" "pobMuj"

selecciona información del año 2000 y se calcula la poblacion total de ese año

df2000<-df %>% filter(year == "2000") # filtra información del año 2000
## Warning: package 'bindrcpp' was built under R version 3.4.4
summarise (df2000, sum_h= sum(pobHom, na.rm = T), sum_m= sum(pobMuj, na.rm = T), pob_2000_murcia= sum(sum_h+sum_m))            # suma la poblacion de hombres y mujeres
##    sum_h  sum_m pob_2000_murcia
## 1 570125 579203         1149328

calcular la poblacion total por cada año y municipio

df$pobTotal<- df$pobHom+df$pobMuj # sumar las dos poblaciones y se crea la variable pobtotal
head(df)
##   codMun      Mun year pobHom pobMuj pobTotal
## 1      1 Abanilla 2000   3050   3056     6106
## 2      1 Abanilla 2001   3030   3003     6033
## 3      1 Abanilla 2002   3045   2995     6040
## 4      1 Abanilla 2003   3151   3088     6239
## 5      1 Abanilla 2004   3084   3061     6145
## 6      1 Abanilla 2005   3171   3094     6265

Se filtro por el municipio Albanilla y se calculo la media de la población de hombres, mujeres y total

dfalbanilla <- df%>% filter (Mun=="Abanilla")
summarise (dfalbanilla, media_pobHom= mean (pobHom), media_pobMuj=mean(pobMuj), media_pobtot=mean (pobTotal))
##   media_pobHom media_pobMuj media_pobtot
## 1     3221.875     3158.688     6380.562

Se realiza el grafico de población de Albanilla

plot( c(2000,2016), c(0,7000), type="n", xlab="Año",
ylab="Número de habitantes", main = "Población de Albanilla" )

lines( x=dfalbanilla$year, y=dfalbanilla$pobMuj, lwd =1, lty = 3, col = "blue" )  
lines( x=dfalbanilla$year, y=dfalbanilla$pobHom, lwd =1, lty = 1, col = "blue" )  
lines( dfalbanilla$year, dfalbanilla$pobTotal,lwd= 3, lty = 1, col = "black" )
legend ("bottomright",
        cex=0.6,
        c("Total", "Hombres", "Mujeres"), 
        horiz= FALSE, 
        col = c("black", "blue", "blue"),
        pch = 15,
        pt.cex = 0.8,
        lty=c(1,1,3),
        lwd=c(3,1,1),
        title ="Poblacionales", fill=c(1, "blue", "blue"))