Exercise 13.

Using the database called ‘survey’ which is part of the ‘MASS’ library

Perform the following actions:

Save the database in a variable called ‘ExamData’. Review the database structure (variable type of each column). Then search and fill in the missing data in the base: omit those rows. Save this new database in a variable called ‘ExamData2’. From the new base (ExamData2) select only the columns: Sex, W.Hnd, Height, Age, Smoke. Then filter those observations in which Smoke has the value “Never”. Sort this database according to the ‘Age’ column (from oldest to youngest) and save the result in a variable called ‘FilteredData’. Age vs Sex Chart

# Cargar la librería MASS y cargar la base de datos 'survey'
library(MASS)
data(survey)

# Paso 1: Guardar la base de datos en una variable llamada 'ExamData'
ExamData <- survey

# Paso 2: Revisar la estructura de la base de datos y llenar los datos faltantes
str(ExamData)
## 'data.frame':    237 obs. of  12 variables:
##  $ Sex   : Factor w/ 2 levels "Female","Male": 1 2 2 2 2 1 2 1 2 2 ...
##  $ Wr.Hnd: num  18.5 19.5 18 18.8 20 18 17.7 17 20 18.5 ...
##  $ NW.Hnd: num  18 20.5 13.3 18.9 20 17.7 17.7 17.3 19.5 18.5 ...
##  $ W.Hnd : Factor w/ 2 levels "Left","Right": 2 1 2 2 2 2 2 2 2 2 ...
##  $ Fold  : Factor w/ 3 levels "L on R","Neither",..: 3 3 1 3 2 1 1 3 3 3 ...
##  $ Pulse : int  92 104 87 NA 35 64 83 74 72 90 ...
##  $ Clap  : Factor w/ 3 levels "Left","Neither",..: 1 1 2 2 3 3 3 3 3 3 ...
##  $ Exer  : Factor w/ 3 levels "Freq","None",..: 3 2 2 2 3 3 1 1 3 3 ...
##  $ Smoke : Factor w/ 4 levels "Heavy","Never",..: 2 4 3 2 2 2 2 2 2 2 ...
##  $ Height: num  173 178 NA 160 165 ...
##  $ M.I   : Factor w/ 2 levels "Imperial","Metric": 2 1 NA 2 2 1 1 2 2 2 ...
##  $ Age   : num  18.2 17.6 16.9 20.3 23.7 ...
ExamData2 <- na.omit(ExamData)

# Paso 3: Seleccionar las columnas requeridas y filtrar según la columna 'Smoke'
FilteredData <- subset(ExamData2, Smoke == "Never", select = c(Sex, W.Hnd, Height, Age, Smoke))
FilteredData <- FilteredData[order(FilteredData$Age), ]

# Paso 4: Crear un gráfico de dispersión de Edad vs Sexo
plot(FilteredData$Age, main = "Graphic Age vs Sex", xlab = "Age", ylab = "Sex", col = as.factor(FilteredData$Sex))
legend("topright", legend = c("Female", "Male"), col = c("brown", "darkgreen"), pch = 1)