0% found this document useful (0 votes)
279 views15 pages

Lab 1

The document describes the analysis and visualization of the VADeaths dataset, which includes death rates categorized by age groups and gender in both rural and urban settings. It includes the creation of histograms to illustrate overall death rates and specific rates for different age groups and demographics. Additionally, it presents a similar analysis of the airquality dataset, focusing on daily maximum temperatures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
279 views15 pages

Lab 1

The document describes the analysis and visualization of the VADeaths dataset, which includes death rates categorized by age groups and gender in both rural and urban settings. It includes the creation of histograms to illustrate overall death rates and specific rates for different age groups and demographics. Additionally, it presents a similar analysis of the airquality dataset, focusing on daily maximum temperatures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

# Load and convert VADeaths dataset

data("VADeaths")

# Convert to data frame


va_df <- as.data.frame(as.table(VADeaths))
colnames(va_df) <- c("AgeGroup", "Group", "DeathRate")
va_df

## AgeGroup Group DeathRate


## 1 50-54 Rural Male 11.7
## 2 55-59 Rural Male 18.1
## 3 60-64 Rural Male 26.9
## 4 65-69 Rural Male 41.0
## 5 70-74 Rural Male 66.0
## 6 50-54 Rural Female 8.7
## 7 55-59 Rural Female 11.7
## 8 60-64 Rural Female 20.3
## 9 65-69 Rural Female 30.9
## 10 70-74 Rural Female 54.3
## 11 50-54 Urban Male 15.4
## 12 55-59 Urban Male 24.3
## 13 60-64 Urban Male 37.0
## 14 65-69 Urban Male 54.6
## 15 70-74 Urban Male 71.1
## 16 50-54 Urban Female 8.4
## 17 55-59 Urban Female 13.6
## 18 60-64 Urban Female 19.3
## 19 65-69 Urban Female 35.1
## 20 70-74 Urban Female 50.0
hist(va_df$DeathRate,
main = "Overall Histogram of Death Rates",
xlab = "Death Rate per 1000",
col = "skyblue",
border = "black")

1
Overall Histogram of Death Rates
6
5
4
Frequency

3
2
1
0

0 20 40 60 80

Death Rate per 1000

age_levels <- unique(va_df$AgeGroup)

for (age in age_levels) {


hist(va_df$DeathRate[va_df$AgeGroup == age],
main = paste("Death Rates - Age Group:", age),
xlab = "Death Rate",
col = "lightgreen",
border = "black")
}

2
Death Rates − Age Group: 50−54
2
Frequency

1
0

8 10 12 14 16

Death Rate

3
Death Rates − Age Group: 55−59
2
Frequency

1
0

10 15 20 25

Death Rate

4
Death Rates − Age Group: 60−64
1
Frequency

15 20 25 30 35 40

Death Rate

5
Death Rates − Age Group: 65−69
2
Frequency

1
0

30 35 40 45 50 55 60

Death Rate

6
Death Rates − Age Group: 70−74
2
Frequency

1
0

50 55 60 65 70 75 80

Death Rate

group_levels <- unique(va_df$Group)

for (grp in group_levels) {


hist(va_df$DeathRate[va_df$Group == grp],
main = paste("Death Rates - Group:", grp),
xlab = "Death Rate",
col = "salmon",
border = "black")
}

7
Death Rates − Group: Rural Male
2
Frequency

1
0

10 20 30 40 50 60 70

Death Rate

8
Death Rates − Group: Rural Female
1
Frequency

0 10 20 30 40 50 60

Death Rate

9
Death Rates − Group: Urban Male
1
Frequency

10 20 30 40 50 60 70 80

Death Rate

10
Death Rates − Group: Urban Female
2
Frequency

1
0

0 10 20 30 40 50

Death Rate

# Layout for 2x2 plots


par(mfrow = c(2, 2))

for (grp in group_levels) {


hist(va_df$DeathRate[va_df$Group == grp],
main = paste(grp),
xlab = "Death Rate",
col = "orange",
border = "black")
}

11
2
Rural Male Rural Female

1
Frequency

Frequency
1
0

0
10 20 30 40 50 60 70 0 10 20 30 40 50 60

Death Rate Death Rate

Urban Male Urban Female


1

2
Frequency

Frequency

1
0

0
10 20 30 40 50 60 70 80 0 10 20 30 40 50

Death Rate Death Rate

# Reset layout
par(mfrow = c(1, 1))

# Layout for 3x2 grid (fits 5 plots)


par(mfrow = c(3, 2))

for (age in age_levels) {


hist(va_df$DeathRate[va_df$AgeGroup == age],
main = paste(age),
xlab = "Death Rate",
col = "lightblue",
border = "black")
}

# Reset layout
par(mfrow = c(1, 1))

12
50−54 55−59
Frequency

Frequency
2

2
0

0
8 10 12 14 16 10 15 20 25

Death Rate Death Rate

60−64 65−69
Frequency

Frequency
1

2
0

0
15 20 25 30 35 40 30 35 40 45 50 55 60

Death Rate Death Rate

70−74
Frequency

2
0

50 55 60 65 70 75 80

Death Rate

# Combine urban and rural groups


urban_rates <- c(VADeaths[, "Urban Male"], VADeaths[, "Urban Female"])
rural_rates <- c(VADeaths[, "Rural Male"], VADeaths[, "Rural Female"])

# Plot together
par(mfrow = c(1, 2))

hist(rural_rates, main = "Rural Death Rates", xlab = "Rate", col = "lightblue")


hist(urban_rates, main = "Urban Death Rates", xlab = "Rate", col = "lightpink")

13
Rural Death Rates Urban Death Rates
3

3
2

2
Frequency

Frequency
1

1
0

0 20 40 60 0 0 20 40 60 80

Rate Rate

par(mfrow = c(1, 1))

data("airquality")
head(airquality)

## Ozone Solar.R Wind Temp Month Day


## 1 41 190 7.4 67 5 1
## 2 36 118 8.0 72 5 2
## 3 12 149 12.6 74 5 3
## 4 18 313 11.5 62 5 4
## 5 NA NA 14.3 56 5 5
## 6 28 NA 14.9 66 5 6
hist(airquality$Temp,
main = "Histogram of Daily Maximum Temperature\nLa Guardia Airport (1973)",
xlab = "Temperature (°F)",
col = "skyblue",
border = "black")

14
Histogram of Daily Maximum Temperature
35
30
25 La Guardia Airport (1973)
Frequency

20
15
10
5
0

60 70 80 90 100

Temperature (°F)

15

You might also like