Question 1: Use the tools of this chapter to rearrange the vectors so that all elements conform to the standard: first_name last_name.
library(stringr)
raw.data <- "555-1239Moe Szyslak(636) 555-0113Burns, C. Montgomery555
-6542Rev. Timothy Lovejoy555 8904Ned Flanders636-555-3226Simpson, Homer5553642Dr. Julius Hibbert"
## Extract only the names from raw data.
firstlast <- unlist(str_extract_all(raw.data, "[[:alpha:]., ]{2,}"))
## Using the sub function to find the title ending with period and remove them.
firstlast2 <- sub("[[:alpha:]]{2,3}\\. ","",firstlast)
## Using the sub function to find the second name ending with period and remove them.
firstlast3 <- sub(" [[:alpha:]]{1,}\\.? "," ",firstlast2)
## Using the sub function to find the last name comma first name and using backreferencing to revert the order and remove the comma.
firstlast4 <-sub("([[:alnum:]_]+),[[:blank:]]([[:alnum:]_]+)","\\2 \\1", firstlast3)
firstlast4
## [1] "Moe Szyslak" "Montgomery Burns" "Timothy Lovejoy"
## [4] "Ned Flanders" "Homer Simpson" "Julius Hibbert"
Question 2: Construct a logical vector indicating whether a character has a little (i.e., Rev.ย and Dr.).
title <- str_detect(firstlast, "[[:alpha:]]{2,3}\\. " )
title2 <- data.frame(firstlast, title)
title2
## firstlast title
## 1 Moe Szyslak FALSE
## 2 Burns, C. Montgomery FALSE
## 3 Rev. Timothy Lovejoy TRUE
## 4 Ned Flanders FALSE
## 5 Simpson, Homer FALSE
## 6 Dr. Julius Hibbert TRUE
Question 3: Construct a logical vector indicating whether a character has a second name.
secondname <- str_detect(firstlast, " [[:alpha:]]{1,}\\. ")
secondname2 <- data.frame(firstlast, secondname)
secondname2
## firstlast secondname
## 1 Moe Szyslak FALSE
## 2 Burns, C. Montgomery TRUE
## 3 Rev. Timothy Lovejoy FALSE
## 4 Ned Flanders FALSE
## 5 Simpson, Homer FALSE
## 6 Dr. Julius Hibbert FALSE
Question 1: [0-9]+\$
library(stringr)
example <- c("1235$", "4343$$")
example2 <- c("I have 9999$")
str_detect(example, "[0-9]+\\$")
## [1] TRUE TRUE
str_extract(example2, "[0-9]+\\$")
## [1] "9999$"
Question 2: \b[a-z]{1,4}\b
library (stringr)
example3 <- c("no", "good")
str_detect(example3, "\\b[a-z]{1,4}\\b")
## [1] TRUE TRUE
str_extract(example3, "\\b[a-z]{1,4}\\b")
## [1] "no" "good"
Question 3: .*?\.txt$
library (stringr)
example4 <- c("4tU.txt","I Have a .txt", " .txt")
str_detect(example4, ".*?\\.txt$")
## [1] TRUE TRUE TRUE
str_extract(example4, ".*?\\.txt$")
## [1] "4tU.txt" "I Have a .txt" " .txt"
Question 4: \d{2}/\d{2}/\d{4}
library (stringr)
example5 <- c("02/13/2005","09/01/2018")
str_detect(example5, "\\d{2}/\\d{2}/\\d{4}")
## [1] TRUE TRUE
str_extract(example5, "\\d{2}/\\d{2}/\\d{4}")
## [1] "02/13/2005" "09/01/2018"
Question 5: <(.+?)>.+?</\1>
library (stringr)
example6 <- c("<tag>Hello World</tag>", "<listen>to wonderful music</listen>")
str_detect(example6, "<(.+?)>.+?</\\1>")
## [1] TRUE TRUE
str_extract(example6, "<(.+?)>.+?</\\1>")
## [1] "<tag>Hello World</tag>"
## [2] "<listen>to wonderful music</listen>"
clcopCow1zmstc0d87wnkig7OvdicpNuggvhryn92Gjuwczi8hqrfpRxs5Aj5dwpn0Tanwo Uwisdij7Lj8kpf03AT5Idr3coc0bt7yczjatOaootj55t3Nj3ne6c4Sfek.r1w1YwwojigO d6vrfUrbz2.2bkAnbhzgv4R9i05zEcrop.wAgnb.SqoU65fPa1otfb7wEm24k6t3sR9zqe5 fy89n6Nd5t9kc4fE905gmc4Rgxo5nhDk!gr
secret_message <- "clcopCow1zmstc0d87wnkig7OvdicpNuggvhryn92Gjuwczi8hqrfpRxs5Aj5dwpn0Tanwo
Uwisdij7Lj8kpf03AT5Idr3coc0bt7yczjatOaootj55t3Nj3ne6c4Sfek.r1w1YwwojigO
d6vrfUrbz2.2bkAnbhzgv4R9i05zEcrop.wAgnb.SqoU65fPa1otfb7wEm24k6t3sR9zqe5
fy89n6Nd5t9kc4fE905gmc4Rgxo5nhDk!gr"
## Extract all letters.
secret_message2 <- unlist(str_extract_all(secret_message,"[[:alpha:]]{1,}"))
## Extract all uppercase letters.
secret_message3 <- unlist(str_extract_all(secret_message2,"[[:upper:]]{1,}"))
## Combine all letters.
secret_message4 <- str_c(secret_message3,collapse="")
## Separate into words.
secret_message5 <-unlist(str_extract_all(secret_message4, "CONGRATULATIONS|YOU|ARE|SUPERNERD"))
## Combine into a single sentence.
secret_message6 <- str_c(secret_message5,collapse=" ")
## Print the secret message.
secret_message6
## [1] "CONGRATULATIONS YOU ARE SUPERNERD"