# stringr and forcats exercises library(tidyverse) library(stringr) library(forcats) #1 Create a string with an embedded double quote #2 Find the length of that string using str_length() #3 Create the string "this is the time" and the string "to do good things" #4 Combine them using a space as a separator #5 investigate the letters data, create a new string vector that prefixes each letter element with "Letter :" #6 Create a list that states the letter that follows it - for example "b follows a", "c follows b" etc. #7 collapse letters to string "abcdefghijklmnopqrstuvwxyz" #8 use string_replace_na on a character vector containing NAs #9 use str_sub to extract the third through sixth characters from a character vector having at least 7 strings #10 use str_to_upper, str_to_lower and str_to_title on any string #11 Investigate str_wrap() and str_trim(). Describe what they do and apply them to a string where these functions have an effect on the string courses <- c("CS2500", "DS2000", "DS4100") #12 match the string DS4100 in the courses vector #13 match all DS courses #14 match the course numbers complex <- c("\\.TO NOT BE", "TO BE\\.", "2 B", NA) writeLines(complex) #15 match all letters in complex #16 match all numbers in complex #17 use anchoring to match the styring "2 B" #18 write 1 regex that matches "TO NOT BE" and "TO BE" abc <- c("abc", "a.c", "a*c", "a c", "a*c") #19 use str_detect to find "a*c" in abc. Use sum to determine how many copies of "a*c" is in the vector, use mean to determine the proportion of strings are "a*c" in abc #20 use the stringr::words vector to find all words that contain no consonants. words_df <- tibble( word = words, i = seq_along(word) ) #21 use filter and str_detect to find all words in words_df ending in x all_a <- c("a", "aa", "aaa", "aaaa") #22 use str_count to count the number of a's in all_a #23 use str_extract and str_extract_all to extract the letters from abc vector #24 replace all of the a's in abc with b's #25 investigate the boundary function and use it with str_split() on your choice of data #forcats #26 create a factor for the likert scale values: strongly agree, aggree, neither agree or disagree, disagree, strongly disagree