Read and Prepare Data

olympic <- read.csv("2024_US_Olympic.csv", stringsAsFactors = FALSE)

#Ensure required columns
req <- c("First_Name","Last_Name","Gender","Hometown_City","Hometown_State","Sport","Event")
for (nm in req) if (!nm %in% names(olympic)) olympic[[nm]] <- NA_character_

#Trim extra spaces in first and last name columns
olympic$First_Name <- gsub("^\\s+|\\s+$", "", olympic$First_Name)
olympic$Last_Name  <- gsub("^\\s+|\\s+$", "", olympic$Last_Name)

head(olympic, 5)
##   First_Name        Last_Name             Sport Hometown_City Hometown_State
## 1      BRADY          ELLISON           Archery      Billings        Montana
## 2   CATALINA         GNORIEGA           Archery     San Diego     California
## 3      CASEY         KAUFHOLD           Archery     Lancaster   Pennsylvania
## 4   JENNIFER MUCINO-FERNANDEZ           Archery   Chula Vista     California
## 5      ANITA          ALVAREZ Artistic Swimming       Buffalo       New York
##   Gender                            Event
## 1   Male                 Men's Individual
## 2 Female Women's Individual, Women's Team
## 3 Female Women's Individual, Women's Team
## 4 Female Women's Individual, Women's Team
## 5 Female                             Team
VOWELS <- "AEIOUaeiou"
CONSONANTS <- "BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz"

starts_with_vowel <- function(x) grepl(paste0("^[", VOWELS, "]"), x)
ends_with_vowel   <- function(x) grepl(paste0("[", VOWELS, "]$"), x)
is_letters_only_one_word <- function(x) grepl("^[A-Za-z]+$", x)
attach(olympic)

#Question 1: There are 100 first names that begin with a vowel. Women are more likely to have a first name with a vowel.

is_vowel_start <- starts_with_vowel(First_Name)
# Total of first names that begin with a vowel
sum(is_vowel_start, na.rm = TRUE)
## [1] 100
# One-way frequency table (gender among those names)
table(Gender[is_vowel_start])
## 
## Female   Male 
##     71     29

#Question 2: There are 211 first names that end with a vowel. Women are more likely to have a first name that ends with a vowel.

is_vowel_end <- ends_with_vowel(First_Name)
# Total
sum(is_vowel_end, na.rm = TRUE)
## [1] 211
# Frequency table by gender
table(Gender[is_vowel_end])
## 
## Female   Male 
##    174     37

#Question 3: There are 31 first names that are not typical.

is_typical_first <- is_letters_only_one_word(First_Name)
# Count
sum(!is_typical_first & !is.na(First_Name))
## [1] 31
# Show all of the unique non-typical names
head(unique(First_Name[!is_typical_first]), 31)
##  [1] "A'JA"               "JOSHUA TIMOTHY"     "MARIA CELIA"       
##  [4] "KATELYN MORGAN"     "RYLAN WILLIAM"      "ADA CLAUDIA"       
##  [7] "HENRY TURNER"       "DERRICK SCOTT"      "RYANN PAIGE"       
## [10] "CONNER LYNN"        "AUSTEN JEWELL"      "RACHEL LEIGHANNE"  
## [13] "MARY CAROLYNN"      "DANIA JO"           "JOHN JOHN"         
## [16] "AALIYAH NICKOLE"    "JAMES ANDRES"       "JEREMIAH JASHON"   
## [19] "ANNETTE NNEKA"      "JASMINE MARI"       "BRYNN TAYLOR"      
## [22] "MONAE'"             "SHA'CARRI"          "GRACE ELIZABETH"   
## [25] "PARKER ALYS"        "ISABELLA MARIE"     "QUINCY ALEXANDER"  
## [28] "NICOLAS MACPHERSON" "EMILY MARY"         "CHASE WILLIAM"     
## [31] "JENNA MICHELLE"

#Question 4:The first name that starts with Z/ZH and ends with Y is Zachary. There are no first names that have more than three vowels in a row or five consonants in a row.

FN_UP <- toupper(First_Name)
# Z / ZH ... Y
head(unique(First_Name[grepl("^Z(H)?", FN_UP) & grepl("Y$", FN_UP)]), 5)
## [1] "ZACHERY"
# >3 vowels in a row
head(unique(First_Name[grepl("[AEIOUaeiou]{4,}", First_Name)]), 5)
## character(0)
# >5 consonants in a row
head(unique(First_Name[grepl(paste0("[", CONSONANTS, "]{6,}"), First_Name)]), 5)
## character(0)

#Question 5: There are 18 last names that are not typical.

is_typical_last <- is_letters_only_one_word(Last_Name)
# Count
sum(!is_typical_last & !is.na(Last_Name))
## [1] 18
# Show all unique non-typical last names
head(unique(Last_Name[!is_typical_last]), 18)
##  [1] "MUCINO-FERNANDEZ"   "VAN LITH"           "GUZZI VINCENTI"    
##  [4] "MAZZIO-MANSON"      "O'CONNOR"           "FUALA'AU"          
##  [7] "DALLMAN-WEISS"      "NEWBERRY MOORE"     "DAVIS-WOODHALL"    
## [10] "KELATI-FREZGHI"     "MALONE-HARDIN"      "MCLAUGHLIN-LEVRONE"
## [13] "O'KEEFFE"           "ST. PIERRE"         "JENDRYK II"        
## [16] "MA'A"               "WONG-ORANTES"       "THEISEN LAPPEN"

#Question 6: The last names that end with TH are Van Lith, Kloth, Leibfarth, Hollingsworth, and Smith. The last name that ends with ZH is Zhang.

LN_UP <- toupper(Last_Name)
# Ends with TH
head(unique(Last_Name[grepl("TH$", LN_UP)]), 5)   
## [1] "VAN LITH"      "KLOTH"         "LEIBFARTH"     "HOLLINGSWORTH"
## [5] "SMITH"
# Starts with ZH
head(unique(Last_Name[grepl("^ZH", LN_UP)]), 5)   
## [1] "ZHANG"

#Question 7: The first five full names are Brady Ellison, Catalina Gnoriega, Casey Kaufhold, Jennifer Mucino-fernandez, and Anita Alvarez.

first_fixed <- paste0(toupper(substr(First_Name,1,1)), tolower(substring(First_Name,2)))
last_fixed  <- paste0(toupper(substr(Last_Name,1,1)),  tolower(substring(Last_Name,2)))
head(paste(first_fixed, last_fixed), 5)
## [1] "Brady Ellison"             "Catalina Gnoriega"        
## [3] "Casey Kaufhold"            "Jennifer Mucino-fernandez"
## [5] "Anita Alvarez"

#Question 8: There are 2 athletes who whose hometown city has more than 20 characters in the city name.

sum(nchar(Hometown_City) > 20, na.rm = TRUE)
## [1] 2

#Question 9: There are 11 athletes whose hometown city has 3 or more words in the city name.

sum(lengths(strsplit(Hometown_City, "\\s+")) >= 3, na.rm = TRUE)
## [1] 11

#Question 10:

has_paren <- grepl("\\(", Sport) & grepl("\\)", Sport)
# Original frequency 
table(Sport[has_paren])
## 
##        Basketball (3x3)        Basketball (5x5)         BMX (Freestyle) 
##                       8                      24                       4 
##            BMX (Racing) Cycling (Mountain Bike) 
##                       5                       4
# Convert "Name (Type)" -> "(Type) Name"
swapped <- Sport
swapped[has_paren] <- sub("^\\s*(.*?)\\s*\\((.*?)\\)\\s*$", "(\\2) \\1", Sport[has_paren])
# First 5 only
head(data.frame(original = Sport[has_paren],
                converted = swapped[has_paren]), 5)
##           original        converted
## 1 Basketball (3x3) (3x3) Basketball
## 2 Basketball (3x3) (3x3) Basketball
## 3 Basketball (3x3) (3x3) Basketball
## 4 Basketball (3x3) (3x3) Basketball
## 5 Basketball (3x3) (3x3) Basketball
# Table
table(swapped[has_paren])
## 
##        (3x3) Basketball        (5x5) Basketball         (Freestyle) BMX 
##                       8                      24                       4 
## (Mountain Bike) Cycling            (Racing) BMX 
##                       4                       5

#Question 11: There are 101 total number of athletes in team events.

team_sports <- c("Basketball","Field Hockey","Rugby","Soccer")

# Search
is_team_word   <- grepl("Team", Event, ignore.case = TRUE, useBytes = TRUE)

# Find exact match
is_gender_only <- (Event == "Male" | Event == "Female" | Event == "male" | Event == "female")

is_team_sport  <- Sport %in% team_sports

is_team_event <- is_team_word | (is_gender_only & is_team_sport)
sum(is_team_event, na.rm = TRUE)
## [1] 101