your_data <- read.csv(“recent-grads.csv”)
majors <- your_data$Major
pattern <- “(DATA|STATISTICS)”
matches <- grepl(pattern, majors, ignore.case = TRUE)
matching_majors <- your_data[matches, ]
cat(“Majors containing ‘DATA’ or ‘STATISTICS’:”) cat(matching_majors$Major, sep = “”)
items <- c(“bell pepper”, “bilberry”, “blackberry”, “blood orange”, “blueberry”, “cantaloupe”, “chili pepper”, “cloudberry”, “elderberry”, “lime”, “lychee”, “mulberry”, “olive”, “salal berry”)
formatted_items <- paste0(“c("”, paste(items, collapse = “", "”), “")”)
cat(formatted_items, “”)
text <- c(“baaaab”, “abbab”, “ababab”, “ababa”, “abcddcba”)
pattern1 <- “(.)\1\1” pattern2 <- “(.)(.)\2\1” pattern3 <- “(..)\1” pattern4 <- “(.).\1.\1” pattern5 <- “(.)(.)(.).*\3\2\1”
cat(“Pattern 1 matches:”, grep(pattern1, text, value = TRUE), “”) cat(“Pattern 2 matches:”, grep(pattern2, text, value = TRUE), “”) cat(“Pattern 3 matches:”, grep(pattern3, text, value = TRUE), “”) cat(“Pattern 4 matches:”, grep(pattern4, text, value = TRUE), “”) cat(“Pattern 5 matches:”, grep(pattern5, text, value = TRUE), “”)
start_end_same_char <- “^([a-zA-Z]).*\1$”
words <- c(“racecar”, “hello”, “apple”, “banana”, “civic”)
matching_words <- grep(start_end_same_char, words, value = TRUE) cat(“Words that start and end with the same character:”, matching_words, “”)
repeated_pair <- “.([a-zA-Z]{2}).\1.*”
words <- c(“church”, “apple”, “banana”, “successful”, “book”)
matching_words <- grep(repeated_pair, words, value = TRUE) cat(“Words that contain a repeated pair of letters:”, matching_words, “”)
word_list <- c(“eleven”, “apple”, “banana”, “committee”, “success”, “address”)
contains_repeated_letter <- function(word) { word <- tolower(word) # Convert the word to lowercase for case insensitivity letters <- strsplit(word, ““)[[1]] # Split the word into letters
# Create a table of letter frequencies letter_counts <- table(letters)
# Check if any letter occurs at least three times any(letter_counts >= 3) }
result <- Filter(contains_repeated_letter, word_list)
cat(“Words containing one letter repeated in at least three places:”, “”) cat(result, sep = “,”)