Homework 1: Load Built-in Dataset – Titanic Dataset

# Install and load package
install.packages("titanic")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.6'
## (as 'lib' is unspecified)
library(titanic)

# Load dataset
titanic <- titanic_train

# Display first rows
head(titanic)
##   PassengerId Survived Pclass
## 1           1        0      3
## 2           2        1      1
## 3           3        1      3
## 4           4        1      1
## 5           5        0      3
## 6           6        0      3
##                                                  Name    Sex Age SibSp Parch
## 1                             Braund, Mr. Owen Harris   male  22     1     0
## 2 Cumings, Mrs. John Bradley (Florence Briggs Thayer) female  38     1     0
## 3                              Heikkinen, Miss. Laina female  26     0     0
## 4        Futrelle, Mrs. Jacques Heath (Lily May Peel) female  35     1     0
## 5                            Allen, Mr. William Henry   male  35     0     0
## 6                                    Moran, Mr. James   male  NA     0     0
##             Ticket    Fare Cabin Embarked
## 1        A/5 21171  7.2500              S
## 2         PC 17599 71.2833   C85        C
## 3 STON/O2. 3101282  7.9250              S
## 4           113803 53.1000  C123        S
## 5           373450  8.0500              S
## 6           330877  8.4583              Q
# Dataset dimensions
dim(titanic)
## [1] 891  12
# Column names
colnames(titanic)
##  [1] "PassengerId" "Survived"    "Pclass"      "Name"        "Sex"        
##  [6] "Age"         "SibSp"       "Parch"       "Ticket"      "Fare"       
## [11] "Cabin"       "Embarked"

Homework 2: Merging Datasets Using 1, 2, and 3 Variables

# Split into two datasets
titanic_personal <- titanic[, c("PassengerId", "Sex", "Age", "Name")]

titanic_travel <- titanic[, c("PassengerId", "Sex",
                              "Pclass", "Fare",
                              "Survived")]

# Merge by 1 variable
merged_1var <- merge(
  titanic_personal,
  titanic_travel,
  by = "PassengerId"
)

head(merged_1var)
##   PassengerId  Sex.x Age                                                Name
## 1           1   male  22                             Braund, Mr. Owen Harris
## 2           2 female  38 Cumings, Mrs. John Bradley (Florence Briggs Thayer)
## 3           3 female  26                              Heikkinen, Miss. Laina
## 4           4 female  35        Futrelle, Mrs. Jacques Heath (Lily May Peel)
## 5           5   male  35                            Allen, Mr. William Henry
## 6           6   male  NA                                    Moran, Mr. James
##    Sex.y Pclass    Fare Survived
## 1   male      3  7.2500        0
## 2 female      1 71.2833        1
## 3 female      3  7.9250        1
## 4 female      1 53.1000        1
## 5   male      3  8.0500        0
## 6   male      3  8.4583        0
# Merge by 2 variables
merged_2var <- merge(
  titanic_personal,
  titanic_travel,
  by = c("PassengerId", "Sex")
)

head(merged_2var)
##   PassengerId    Sex Age                                Name Pclass    Fare
## 1           1   male  22             Braund, Mr. Owen Harris      3  7.2500
## 2          10 female  14 Nasser, Mrs. Nicholas (Adele Achem)      2 30.0708
## 3         100   male  34                   Kantor, Mr. Sinai      2 26.0000
## 4         101 female  28             Petranec, Miss. Matilda      3  7.8958
## 5         102   male  NA    Petroff, Mr. Pastcho ("Pentcho")      3  7.8958
## 6         103   male  21           White, Mr. Richard Frasar      1 77.2875
##   Survived
## 1        0
## 2        1
## 3        0
## 4        0
## 5        0
## 6        0
# Add Pclass to personal dataset
titanic_personal <- titanic[, c("PassengerId", "Sex",
                                "Pclass", "Age", "Name")]

# Merge by 3 variables
merged_3var <- merge(
  titanic_personal,
  titanic_travel,
  by = c("PassengerId", "Sex", "Pclass")
)

head(merged_3var)
##   PassengerId    Sex Pclass Age                                Name    Fare
## 1           1   male      3  22             Braund, Mr. Owen Harris  7.2500
## 2          10 female      2  14 Nasser, Mrs. Nicholas (Adele Achem) 30.0708
## 3         100   male      2  34                   Kantor, Mr. Sinai 26.0000
## 4         101 female      3  28             Petranec, Miss. Matilda  7.8958
## 5         102   male      3  NA    Petroff, Mr. Pastcho ("Pentcho")  7.8958
## 6         103   male      1  21           White, Mr. Richard Frasar 77.2875
##   Survived
## 1        0
## 2        1
## 3        0
## 4        0
## 5        0
## 6        0

Homework 3: Grouping Using $ and %>%and additional dplyr Functions

# Load dplyr
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
# Using $
mean(titanic$Age, na.rm = TRUE)
## [1] 29.69912
table(titanic$Sex)
## 
## female   male 
##    314    577
# Using %>%
titanic %>%
  group_by(Pclass, Sex) %>%
  summarise(
    avg_age       = mean(Age, na.rm = TRUE),
    avg_fare      = mean(Fare, na.rm = TRUE),
    survival_rate = mean(Survived, na.rm = TRUE),
    total         = n()
  )
## `summarise()` has regrouped the output.
## ℹ Summaries were computed grouped by Pclass and Sex.
## ℹ Output is grouped by Pclass.
## ℹ Use `summarise(.groups = "drop_last")` to silence this message.
## ℹ Use `summarise(.by = c(Pclass, Sex))` for per-operation grouping
##   (`?dplyr::dplyr_by`) instead.
## # A tibble: 6 × 6
## # Groups:   Pclass [3]
##   Pclass Sex    avg_age avg_fare survival_rate total
##    <int> <chr>    <dbl>    <dbl>         <dbl> <int>
## 1      1 female    34.6    106.          0.968    94
## 2      1 male      41.3     67.2         0.369   122
## 3      2 female    28.7     22.0         0.921    76
## 4      2 male      30.7     19.7         0.157   108
## 5      3 female    21.8     16.1         0.5     144
## 6      3 male      26.5     12.7         0.135   347
# filter() — keep only rows that meet a condition
titanic %>%
  filter(Sex == "female" & Age > 18) %>%
  head(10)
##    PassengerId Survived Pclass
## 1            2        1      1
## 2            3        1      3
## 3            4        1      1
## 4            9        1      3
## 5           12        1      1
## 6           16        1      2
## 7           19        0      3
## 8           26        1      3
## 9           41        0      3
## 10          42        0      2
##                                                         Name    Sex Age SibSp
## 1        Cumings, Mrs. John Bradley (Florence Briggs Thayer) female  38     1
## 2                                     Heikkinen, Miss. Laina female  26     0
## 3               Futrelle, Mrs. Jacques Heath (Lily May Peel) female  35     1
## 4          Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg) female  27     0
## 5                                   Bonnell, Miss. Elizabeth female  58     0
## 6                           Hewlett, Mrs. (Mary D Kingcome)  female  55     0
## 7    Vander Planke, Mrs. Julius (Emelia Maria Vandemoortele) female  31     1
## 8  Asplund, Mrs. Carl Oscar (Selma Augusta Emilia Johansson) female  38     1
## 9             Ahlin, Mrs. Johan (Johanna Persdotter Larsson) female  40     1
## 10  Turpin, Mrs. William John Robert (Dorothy Ann Wonnacott) female  27     1
##    Parch           Ticket    Fare Cabin Embarked
## 1      0         PC 17599 71.2833   C85        C
## 2      0 STON/O2. 3101282  7.9250              S
## 3      0           113803 53.1000  C123        S
## 4      2           347742 11.1333              S
## 5      0           113783 26.5500  C103        S
## 6      0           248706 16.0000              S
## 7      0           345763 18.0000              S
## 8      5           347077 31.3875              S
## 9      0             7546  9.4750              S
## 10     0            11668 21.0000              S
# arrange() — sort rows
titanic %>%
  arrange(desc(Fare)) %>%
  head(5)
##   PassengerId Survived Pclass                               Name    Sex Age
## 1         259        1      1                   Ward, Miss. Anna female  35
## 2         680        1      1 Cardeza, Mr. Thomas Drake Martinez   male  36
## 3         738        1      1             Lesurer, Mr. Gustave J   male  35
## 4          28        0      1     Fortune, Mr. Charles Alexander   male  19
## 5          89        1      1         Fortune, Miss. Mabel Helen female  23
##   SibSp Parch   Ticket     Fare       Cabin Embarked
## 1     0     0 PC 17755 512.3292                    C
## 2     0     1 PC 17755 512.3292 B51 B53 B55        C
## 3     0     0 PC 17755 512.3292        B101        C
## 4     3     2    19950 263.0000 C23 C25 C27        S
## 5     3     2    19950 263.0000 C23 C25 C27        S
# rename() — rename columns
titanic %>%
  rename(
    Gender = Sex,
    TicketClass = Pclass
  ) %>%
  head(5)
##   PassengerId Survived TicketClass
## 1           1        0           3
## 2           2        1           1
## 3           3        1           3
## 4           4        1           1
## 5           5        0           3
##                                                  Name Gender Age SibSp Parch
## 1                             Braund, Mr. Owen Harris   male  22     1     0
## 2 Cumings, Mrs. John Bradley (Florence Briggs Thayer) female  38     1     0
## 3                              Heikkinen, Miss. Laina female  26     0     0
## 4        Futrelle, Mrs. Jacques Heath (Lily May Peel) female  35     1     0
## 5                            Allen, Mr. William Henry   male  35     0     0
##             Ticket    Fare Cabin Embarked
## 1        A/5 21171  7.2500              S
## 2         PC 17599 71.2833   C85        C
## 3 STON/O2. 3101282  7.9250              S
## 4           113803 53.1000  C123        S
## 5           373450  8.0500              S
# mutate() — create a new column
titanic %>%
  mutate(FarePerAge = Fare / Age) %>%
  head(5)
##   PassengerId Survived Pclass
## 1           1        0      3
## 2           2        1      1
## 3           3        1      3
## 4           4        1      1
## 5           5        0      3
##                                                  Name    Sex Age SibSp Parch
## 1                             Braund, Mr. Owen Harris   male  22     1     0
## 2 Cumings, Mrs. John Bradley (Florence Briggs Thayer) female  38     1     0
## 3                              Heikkinen, Miss. Laina female  26     0     0
## 4        Futrelle, Mrs. Jacques Heath (Lily May Peel) female  35     1     0
## 5                            Allen, Mr. William Henry   male  35     0     0
##             Ticket    Fare Cabin Embarked FarePerAge
## 1        A/5 21171  7.2500              S  0.3295455
## 2         PC 17599 71.2833   C85        C  1.8758763
## 3 STON/O2. 3101282  7.9250              S  0.3048077
## 4           113803 53.1000  C123        S  1.5171429
## 5           373450  8.0500              S  0.2300000

Homework 4: Function with Conditional Logic (Dataset Column Summary)

# Define a function that takes a dataset and column name as parameters
# It automatically detects if the column is numeric or categorical

column_summary <- function(data, col_name) {
  
  x <- data[[col_name]]
  
  # If the column is numeric, calculate statistical summaries
  if (is.numeric(x)) {
    
    cat("\nSummary for", col_name, "\n")
    cat("Mean:", mean(x, na.rm = TRUE), "\n")
    cat("Median:", median(x, na.rm = TRUE), "\n")
    cat("Minimum:", min(x, na.rm = TRUE), "\n")
    cat("Maximum:", max(x, na.rm = TRUE), "\n")
    cat("Standard Deviation:", sd(x, na.rm = TRUE), "\n")
    
  } else {
    
    # If the column is categorical, show a frequency table
    cat("\nFrequency table for", col_name, "\n")
    print(table(x))
  }
}

# Numeric columns — returns statistical summary
column_summary(titanic, "Age")
## 
## Summary for Age 
## Mean: 29.69912 
## Median: 28 
## Minimum: 0.42 
## Maximum: 80 
## Standard Deviation: 14.5265
column_summary(titanic, "Fare")
## 
## Summary for Fare 
## Mean: 32.20421 
## Median: 14.4542 
## Minimum: 0 
## Maximum: 512.3292 
## Standard Deviation: 49.69343
column_summary(titanic, "Survived")
## 
## Summary for Survived 
## Mean: 0.3838384 
## Median: 0 
## Minimum: 0 
## Maximum: 1 
## Standard Deviation: 0.4865925
# Categorical columns — returns frequency table
column_summary(titanic, "Sex")
## 
## Frequency table for Sex 
## x
## female   male 
##    314    577
column_summary(titanic, "Pclass")
## 
## Summary for Pclass 
## Mean: 2.308642 
## Median: 3 
## Minimum: 1 
## Maximum: 3 
## Standard Deviation: 0.8360712
column_summary(titanic, "Embarked")
## 
## Frequency table for Embarked 
## x
##       C   Q   S 
##   2 168  77 644

Homework 5: Using Apply Functions

# Extract numeric columns
x_numeric <- titanic[, sapply(titanic, is.numeric)]

# lapply() — returns a list
lapply(
  x_numeric,
  mean,
  na.rm = TRUE
)
## $PassengerId
## [1] 446
## 
## $Survived
## [1] 0.3838384
## 
## $Pclass
## [1] 2.308642
## 
## $Age
## [1] 29.69912
## 
## $SibSp
## [1] 0.5230079
## 
## $Parch
## [1] 0.3815937
## 
## $Fare
## [1] 32.20421
# sapply() — returns a named vector
sapply(
  x_numeric,
  mean,
  na.rm = TRUE
)
## PassengerId    Survived      Pclass         Age       SibSp       Parch 
## 446.0000000   0.3838384   2.3086420  29.6991176   0.5230079   0.3815937 
##        Fare 
##  32.2042080
# vapply() — safer and strict return type
vapply(
  x_numeric,
  mean,
  numeric(1),
  na.rm = TRUE
)
## PassengerId    Survived      Pclass         Age       SibSp       Parch 
## 446.0000000   0.3838384   2.3086420  29.6991176   0.5230079   0.3815937 
##        Fare 
##  32.2042080
# tapply() — average fare grouped by passenger class
tapply(
  titanic$Fare,
  titanic$Pclass,
  mean,
  na.rm = TRUE
)
##        1        2        3 
## 84.15469 20.66218 13.67555
# mapply() — apply function over two columns
mapply(
  function(x, y) x + y,
  titanic$Age,
  titanic$Fare
)
##   [1]  29.2500 109.2833  33.9250  88.1000  43.0500       NA 105.8625  23.0750
##   [9]  38.1333  44.0708  20.7000  84.5500  28.0500  70.2750  21.8542  71.0000
##  [17]  31.1250       NA  49.0000       NA  61.0000  47.0000  23.0292  63.5000
##  [25]  29.0750  69.3875       NA 282.0000       NA       NA  67.7208       NA
##  [33]       NA  76.5000 110.1708  94.0000       NA  29.0500  36.0000  25.2417
##  [41]  49.4750  48.0000       NA  44.5792  26.8792       NA       NA       NA
##  [49]       NA  35.8000  46.6875  28.8000 125.7292  55.0000 126.9792       NA
##  [57]  31.5000  35.7292  32.7500  57.9000  29.2292 118.0000 128.4750  31.9000
##  [65]       NA       NA  39.5000  27.1583  24.9250  34.6625  42.5000  62.9000
##  [73]  94.5000  40.4542  88.4958  32.6500       NA       NA  29.8300  42.4750
##  [81]  31.0000  38.5000       NA  75.1000  27.5000  48.8500  50.3750       NA
##  [89] 286.0000  32.0500  37.0500  27.8542 107.1750  46.5750  66.2500       NA
##  [97] 105.6542  86.3583  57.0000  60.0000  35.8958       NA  98.2875  41.6542
## [105]  44.9250  35.8958  28.6500       NA  45.8958       NA  99.0000  28.9542
## [113]  30.0500  29.8250  31.4583  28.9250  78.2500  50.0000 271.5208  33.2750
## [121]  94.5000       NA  62.5708  45.5000 131.2875  23.2417       NA  31.1417
## [129]       NA  51.9750  40.8958  27.0500  61.5000  55.0000  38.0000  38.0458
## [137]  45.2833  90.1000  25.2167 103.2000       NA  29.7500  39.8500  25.7500
## [145]  29.5000  55.7500  34.7958  43.3750  62.5000  55.0000  63.5250  88.6000
## [153]  63.5500  55.0000       NA 112.3792  23.7333  38.0500       NA       NA
## [161]  60.1000  55.7500  33.7750  25.6625  40.6875  29.5250       NA  72.9000
## [169]       NA  84.4958  94.5000  33.1250  12.1333  28.9250  86.6958  25.8542
## [177]       NA  78.7125  43.0000  36.0000       NA       NA  40.3875  40.0000
## [185]  26.0250       NA       NA  71.5500  55.5000  43.8958  45.0000  32.0000
## [193]  26.8542  29.0000  71.7208 204.5208       NA  50.4042       NA  37.0000
## [201]  37.5000       NA  40.4958  52.7250  26.0500  12.4625  47.8500  44.7875
## [209]  23.7500  71.0000  31.0500  56.0000  29.2500  43.0000       NA 144.2750
## [217]  34.9250  69.0000 108.2917  40.5000  24.0500  40.0000  59.0500       NA
## [225] 128.0000  31.3500  29.5000  27.7500  31.0000       NA 118.4750  36.7750
## [233]  72.5000  36.3875  34.5000       NA  70.0000  34.2500  29.5000  45.2750
## [241]       NA       NA  39.5000  29.1250  37.2250 134.0000  32.7750  38.5000
## [249]  89.5542  80.0000       NA  39.4625  88.5500  46.1000  61.2125  44.2458
## [257]       NA 116.5000 547.3292  76.0000       NA  34.3875 131.6500  40.0000
## [265]       NA  46.5000  55.6875  32.7750 211.4625 170.6333       NA  25.0000
## [273]  60.5000  66.7000       NA 140.9583  52.7500       NA  36.1250  55.2500
## [281]  72.7500  35.8542  25.5000  27.0500       NA  41.6625  39.5000  29.8958
## [289]  55.0000  29.7500 104.8500 110.0792  48.8750  32.8500  31.8958       NA
## [297]  30.7292 153.5500       NA 297.5208       NA       NA  19.0000       NA
## [305]       NA 152.4700       NA 125.9000  54.0000  86.9292 107.1583 280.3750
## [313]  52.0000  35.8958  69.2500  33.8542  50.0000  68.0000 195.8667 174.5000
## [321]  29.2500  34.8958  42.3500  51.0000       NA 171.6333  67.2375  49.0000
## [329]  51.5250  73.9792       NA  74.0000 191.4625  34.0000       NA       NA
## [337]  95.6000 175.5000  53.0500  80.5000  28.0000 287.0000  41.0000  38.0000
## [345]  49.0000  37.0000  53.0000       NA  18.9000  50.6625  32.2250       NA
## [353]  22.2292  42.8000       NA  37.5000  77.0000  51.0000       NA       NA
## [361]  67.9000  56.7208  59.4542  42.0500       NA  37.2500 135.2500       NA
## [369]       NA  93.3000  80.4417  24.4958  27.0500 157.6333  24.0750       NA
## [377]  29.2500 238.5000  24.0125  26.7750 269.5250  16.7417  39.9250  87.0000
## [385]       NA  91.5000  47.9000  49.0000       NA  29.0000 156.0000  28.7958
## [393]  35.9250 136.2750  40.7000  29.7958  38.8542  72.0000  33.5000  40.6500
## [401]  46.9250  34.0500  30.8250  43.8500  28.6625  55.0000  58.7500  21.7500
## [409]  28.7750       NA       NA       NA 123.0000       NA  51.9250       NA
## [417]  66.5000  31.0000  43.0000  34.1500       NA  28.7333  36.8750  42.4000
## [425]  38.2125       NA  54.0000  45.0000       NA  40.0500  54.5500       NA
## [433]  68.0000  24.1250 105.9000 134.0000  55.3750  42.7500 327.0000  41.5000
## [441]  71.2500  29.5000  32.7750  41.0000       NA  85.8583  32.5000  60.5500
## [449]  24.2583  82.5000  63.7500       NA  57.7500 138.1042       NA  36.8958
## [457]  91.5500       NA  60.5000       NA  74.5500  42.0500  85.5000  61.0000
## [465]       NA  45.0500       NA  82.5500       NA  20.0083       NA  46.6625
## [473]  60.7500  36.7917  31.8375       NA  55.0000  36.0458  29.5208  14.2875
## [481]  55.9000       NA  58.0500  72.5875 116.0792       NA 125.0000  87.7000
## [489]  38.0500  24.9000       NA  28.2500  85.5000 120.5042  29.0500       NA
## [497] 132.2667       NA 176.5500  31.7958  25.6625  28.7500       NA  46.5875
## [505] 102.5000 126.9000  59.0000       NA  50.5250  82.4958  36.7500       NA
## [513]  62.2875 113.4000  31.4958  81.0208  44.5000       NA  62.0000  39.8958
## [521] 123.5000  29.8958       NA 101.9792       NA  48.2500  60.5000       NA
## [529]  46.9250  34.5000  28.0000       NA  24.2292       NA  38.6625  33.2500
## [537]  71.5500 136.4250       NA  71.5000 107.0000  40.2750  42.2750  58.0000
## [545] 156.4250  90.0000  45.0000       NA  53.5250  44.7500 127.8833  53.0000
## [553]       NA  29.2250  29.7750  88.5500  87.6000       NA 118.6500  53.4000
## [561]       NA  47.8958  41.5000       NA       NA  48.1500  26.8958  50.0750
## [569]       NA  39.8542  72.5000 104.4792  62.3875       NA  24.0500  33.5000
## [577]  47.0000  94.9000       NA  39.9250  55.0000 149.8833  80.0000  76.1250
## [585]       NA  97.6500  62.0000 139.2000  30.0500       NA  42.1250 130.2667
## [593]  54.2500       NA  63.0000  60.1500       NA  49.0000       NA 105.9292
## [601]  51.0000       NA       NA  52.0500  61.5500  51.5500  37.8958  57.5000
## [609]  63.5792 193.4625  70.2750       NA       NA       NA  43.0500  89.0000
## [617]  48.4000  42.1000  43.0000  36.5000  41.4542  94.5542  35.7417  28.8542
## [625]  37.1000  93.3208  69.3500  98.9583  33.8958       NA 110.0000  58.0542
## [633]  62.5000       NA  36.9000  41.0000  39.9250  57.2500  80.6875       NA
## [641]  27.8542  93.3000  29.9000       NA  20.0083 124.7292  26.8958  91.5000
## [649]       NA  30.5500       NA  41.0000  29.4333       NA  24.7500  97.5000
## [657]       NA  47.5000  36.0000 171.2750 183.6500  47.2250  72.5875  43.4958
## [665]  27.9250 105.5000  38.0000       NA  51.0500       NA  79.0000  83.0000
## [673]  80.5000  44.0000       NA  25.7750  32.5500  27.8417  89.9000 548.3292
## [681]       NA 103.7292  29.2250  60.9000  99.0000  66.5792  53.6875  29.1708
## [689]  25.7958 226.3375  88.0000  17.4167       NA  32.2250  86.5500  65.5000
## [697]  52.0500       NA 159.8833  49.6500 245.5250  61.2875  32.4542  32.7417
## [705]  33.8542  65.0000  58.5000  68.2875 173.5500       NA  73.5042       NA
## [713] 100.0000  38.4833  65.0000  26.6500 265.5250  37.5000       NA  40.7750
## [721]  39.0000  24.0542  47.0000  63.0000  80.1000  28.6625  51.0000       NA
## [729]  51.0000  32.9250 240.3375  29.7875       NA  36.0000  36.0000  44.6000
## [737]  82.3750 547.3292       NA       NA       NA 114.8500 283.3750  40.1000
## [745]  38.9250 141.0000  36.2500  43.0000  72.1000  38.7500  27.0000  18.4750
## [753]  42.5000  30.8958 113.0000  15.1700  35.7958  29.5000  42.0500 119.5000
## [761]       NA  48.1250  27.2292 156.0000  23.7750 128.9583       NA  38.2500
## [769]       NA  40.3625  33.5000  55.8542  67.5000       NA  77.0000  25.7500
## [777]       NA  17.4750       NA 254.3375  20.2292  74.0000  59.0000       NA
## [785]  32.0500  32.2500  25.4958  37.1250  21.5750 125.2000       NA  42.0000
## [793]       NA       NA  32.8958  52.0000  74.9292  39.6833  37.2292  54.1500
## [801]  47.0000  57.2500 131.0000   8.9367  33.9750  38.7750  39.0000  25.7750
## [809]  52.0000  86.1000  33.8875  63.1500  45.5000  37.2750  38.5500       NA
## [817]  30.9250  68.0042  49.4500  37.9000 145.5000  35.6625  38.0000  39.4750
## [825]  41.6875       NA       NA  38.0042       NA 142.0000  29.4542  19.5800
## [833]       NA  30.8542  26.3000 122.1583  29.6625       NA  88.4958       NA
## [841]  27.9250  26.5000  61.0000  40.9375  25.6625  49.5500       NA  42.8958
## [849]  61.0000       NA  35.2750  81.7750  24.2458  55.4000  70.0000  27.3500
## [857] 209.8667  77.5500  43.2583       NA  55.1083  32.5000  73.9292       NA
## [865]  37.0000  55.0000  40.8583  81.4958       NA  15.1333  33.8958  99.5542
## [873]  38.0000  56.0000  52.0000  22.2250  29.8458  26.8958       NA 139.1583
## [881]  51.0000  40.8958  32.5167  38.5000  32.0500  68.1250  40.0000  49.0000
## [889]       NA  56.0000  39.7500

Homework 6: Debugging Functions in R Using trace() and recover()

# Define a function that calculates fare per age
fare_per_age <- function(fare, age) {

  if (age == 0)
    stop("Age cannot be zero!")

  result <- fare / age

  return(result)
}

# trace() monitors every time the function is called
trace(
  fare_per_age,

  quote(
    cat(
      "fare_per_age() called with fare =",
      fare,
      "and age =",
      age,
      "\n"
    )
  )
)
## [1] "fare_per_age"
# Call function normally
fare_per_age(100, 25)
## Tracing fare_per_age(100, 25) on entry 
## fare_per_age() called with fare = 100 and age = 25
## [1] 4
# Set recover() as the error handler
options(error = recover)

# Trigger an error because age = 0
try(fare_per_age(50, 0))
## Tracing fare_per_age(50, 0) on entry 
## fare_per_age() called with fare = 50 and age = 0 
## Error in fare_per_age(50, 0) : Age cannot be zero!
# Remove trace and reset error handling
untrace(fare_per_age)

options(error = NULL)

Conclusion

This homework demonstrated how to: