This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
library(haven)
## Warning: package 'haven' was built under R version 4.6.2
lapop23 <- read_sav("lapop23.sav")
# Práctica individual No. 3
library(dplyr)
##
## Adjuntando el paquete: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
### Punto 1
df <- lapop23 %>%
select(q2, vic1ext, aoj11) %>%
filter(
q2 >= 18 & q2 <= 87,
vic1ext >= 1 & vic1ext <= 2,
aoj11 >= 1 & aoj11 <= 4
)
glimpse(df)
## Rows: 1,492
## Columns: 3
## $ q2 <dbl+lbl> 24, 43, 60, 50, 58, 26, 30, 27, 48, 47, 45, 18, 82, 47, 73…
## $ vic1ext <dbl+lbl> 1, 2, 2, 1, 2, 2, 2, 2, 1, 2, 1, 1, 2, 1, 2, 2, 1, 2, 2, 2…
## $ aoj11 <dbl+lbl> 4, 4, 2, 2, 1, 3, 1, 3, 3, 1, 2, 2, 4, 4, 2, 2, 4, 2, 3, 3…
### Punto 2
df$vic1ext <- factor(
df$vic1ext,
levels = c(1, 2),
labels = c("SÃ", "No")
)
df$aoj11 <- factor(
df$aoj11,
levels = c(1, 2, 3, 4),
labels = c("Muy seguro(a)", "Algo seguro(a)",
"Algo inseguro(a)", "Muy inseguro(a)"),
ordered = TRUE
)
table(df$vic1ext)
##
## SÃ No
## 421 1071
table(df$aoj11)
##
## Muy seguro(a) Algo seguro(a) Algo inseguro(a) Muy inseguro(a)
## 203 451 464 374
### Punto 3
round(mean(df$q2), 2) # Media: 39,69 años
## [1] 39.69
median(df$q2) # Mediana: 38 años
## [1] 38
round(sd(df$q2), 2) # Desviación estándar muestral: 15,27 años
## [1] 15.27
min(df$q2) # Edad mÃnima: 18 años
## <labelled<double>[1]>: Edad
## [1] 18
##
## Labels:
## value label
## 888888 DK
## 988888 NR
## 999999 N/A
max(df$q2) # Edad máxima: 87 años
## <labelled<double>[1]>: Edad
## [1] 87
##
## Labels:
## value label
## 888888 DK
## 988888 NR
## 999999 N/A
### Punto 4
tabla_contingencia <- table(df$vic1ext, df$aoj11)
print(tabla_contingencia)
##
## Muy seguro(a) Algo seguro(a) Algo inseguro(a) Muy inseguro(a)
## SÃ 26 100 137 158
## No 177 351 327 216
prueba_chi2 <- chisq.test(tabla_contingencia)
print(prueba_chi2)
##
## Pearson's Chi-squared test
##
## data: tabla_contingencia
## X-squared = 68.664, df = 3, p-value = 8.249e-15
### Punto 5
library(ggpattern)
## Warning: package 'ggpattern' was built under R version 4.6.2
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.6.2
library(hrbrthemes)
## Warning: package 'hrbrthemes' was built under R version 4.6.2
# 1. Crear la tabla de frecuencias absolutas
tabla_seguridad <- as.data.frame(table(df$aoj11))
colnames(tabla_seguridad) <- c("Seguridad", "Frecuencia")
# 2. Elaborar el gráfico de barras con texturas
ggplot(tabla_seguridad, aes(x=Seguridad, y=Frecuencia)) +
geom_col_pattern(
aes(pattern=Seguridad,
pattern_angle=Seguridad,
pattern_spacing=Seguridad
),
fill = 'white',
colour = 'black',
pattern_density = 0.5,
pattern_fill = 'black',
pattern_colour = 'darkgrey'
) +
theme_bw()
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.