```{r setup, include=FALSE}
chooseCRANmirror(graphics = FALSE, ind = 1)
knitr::opts_chunk$set(echo = TRUE,
                      message = FALSE,
                      warning = FALSE,
                      out.width="100%")

if (!require(pacman)) install.packages("pacman")

p_load(
  lubridate,
  here,
  rio,
  tidyverse
)

# Load the wvs dataset
wvs <- import("https://drhuyue.site:10002/Adrian/data/wvs7.rda")
```

## Pre-class Programming Exercises - Week 1

### 1. Function Help and Usage

Display the help documentation for the `mean()` function. Then, use `mean()` to calculate the average age in the `wvs` dataset, excluding any missing values. Provide the code and the result.

```{r mean-help-advanced, exercise=TRUE}
# Write your code here

```

```{r mean-help-advanced-solution}
# Display help
?mean

# Calculate mean age
mean_age <- mean(wvs$age, na.rm = TRUE)
print(paste("The mean age is:", round(mean_age, 2)))
```

### 2. Variable Naming Rules

Create one variable that follows the naming rules and one that doesn't.

```{r variable-naming, exercise=TRUE}
# Create a variable that follows the rules

# Create a variable that doesn't follow the rules

```

```{r variable-naming-solution}
# Variable that follows the rules
valid_variable <- 1:5

# Variable that doesn't follow the rules (this line will produce an error)
# 1invalid_variable <- 1:5
```

### 3. Data Structure Exploration

Use appropriate functions to view the number of rows, columns, and variable names of the `wvs` data frame.

```{r data-structure, exercise=TRUE}
# Write your code here

```

```{r data-structure-solution}
nrow(wvs)  # Number of rows
ncol(wvs)  # Number of columns
names(wvs)  # Variable names
```

### 4. Variable Extraction

Extract the "age" variable from the `wvs` data frame using two different methods.

```{r variable-extraction, exercise=TRUE}
# Method 1

# Method 2

```

```{r variable-extraction-solution}
# Method 1
wvs[, "age"]

# Method 2
wvs$age
```

### 5. Variable Characteristics

Use appropriate functions to examine the characteristics of the "age" variable in the `wvs` data frame.

```{r variable-characteristics, exercise=TRUE}
# Write your code here

```

```{r variable-characteristics-solution}
summary(wvs$age)
table(wvs$age)
```