Exercises

  1. Write a function called revStr() that reverses the characters of any string that it is given. The function should take a single parameter:

    • str: a character-vector of length 1 (a single string).

    Typical examples of use should look like this:

    revStr(str = "goodbye")
    ## [1] "eybdoog"

    Hint: Let’s think about how to solve the reversal problem for a specific string, e.g.:

    str <- "goodbye"

    First, we could turn the string into a list whose only element is the vector of the characters of the string, as follows:

    splitString <- str_split(str, pattern = "")
    splitString
    ## [[1]]
    ## [1] "g" "o" "o" "d" "b" "y" "e"

    This could be turned into just the desired vector with the unlist() function:

    unlist(splitString)
    ## [1] "g" "o" "o" "d" "b" "y" "e"

    Next, recall that R has a function rev() that, when given a vector, returns a vector with the elements in reverse order:

    rev(unlist(splitString))
    ## [1] "e" "y" "b" "d" "o" "o" "g"

    Finally, we would need to convert the reversed vector back into a single string. You have learned a stringr function that will accomplish this.

    After you have solved the problem for the specific vector str, encapsulate your work into the function revStr().

  2. A string is said to be a palindrome if it is the same no matter whether it is spelled backwards or forwards. Write a function called palindromeStr() that determines whether or not a given string is a palindrome. The function should take a single parameter:

    • str: a character-vector of length 1 (a single string).

    It should return TRUE if str is a palindrome, and return FALSE otherwise. Typical example of use should look like this:

    palindromeStr(str = "abba")
    ## [1] TRUE
    palindromeStr("hello")
    ## [1] FALSE

    Hint; Again, you should begin by solving the problem on a specific vector, and only then encapulate your work into a function. To solve the specific problem, you might use the function revStr() from the previous problem. Another possibility is to use rev() along with the function all() that you met in Chapter 2.

  3. Write a function called subStrings() that returns a vector of the substrings of a given string that have at least a given number of characters. The function should take two arguments:

    • str: a character-vector of length 1 (a single string);
    • n: the minimum number of characters a substring should have in order to be included in the vector.

    Validate the input: if the argument for n is less than 1 or greater than the number of characters in str, then the function should advise the user and cease execution. Typical examples of use should look like this (although it is OK if your output-vector contains the sub-strings in a different order):

    subStrings("hello", 3)
    ## [1] "hello" "hell"  "ello"  "hel"   "ell"   "llo"
    subStrings("hello", 6)
    ## n should be at least 1 and no more than the number
    ## of characters in str.

    Hint: Begin by writing a function, called perhaps subStringFixed(), that when given a string and a specific number \(n\), returns all of the substrongs of length exactly \(n\). It might work like this:

    subStringFixed(str = "yabbadabbadoo!", n = 6)
    ## [1] "yabbad" "abbada" "bbadab" "badabb" "adabba" "dabbad" "abbado" "bbadoo" "badoo!"
    subStringFixed(str = "yabbadabbadoo!", n = 0)
    ## n should be at least 1 and no more than the number
    ## of characters in str.
  4. Write a function called subPalindrome() that, for any given string and specified number \(n\), returns a character vector of all the substrings of the string having at least \(n\) characters that are also palindromes. The function should take two arguments:

    • str: a character-vector of length 1 (a single string);
    • n: the minimum number of characters a substring should have in order to be included in the vector.

    Validate the input: if the argument for n is less than 1 or greater than the number of characters in str, then the function should advise the user and cease execution. Typical examples of use should look like this (although it is OK if your output-vector contains the palindromes in a different order):

    ## Note that palindrome substrings are repeated as many times
    ## as they occur in the given string:
    subPalindrome("yabbadabbadoo!", 2)
    ##  [1] "abbadabba" "bbadabb"   "dabbad"    "badab"     "abba"      "abba"      "ada"      
    ##  [8] "bb"        "bb"        "oo"
    subPalindrome("yabbadabbadoo!", 10)
    ## character(0)
    subPalindrome("yabbadabbadoo!", 0)
    ## n should be at least 1 and no more than the number
    ## of characters in str.
  5. Write a function called m111Report() that performs formatted printing from the data frame m111survey in the bcscr package. Given a vector of row numbers, the function will print out the sex, feeling about weight, and GPA of the corresponding individuals. Thus each row in the printout will correspond to an individual in the study. Each row will consist of three fields:

    • The first field is 10 characters wide, and contains either “male” or “female,” followed by the appropriate number of spaces.
    • The first field is 15 characters wide, and contains either “underweight” or “about right” or “overweight,” followed by the appropriate number of spaces.
    • The third field is 5 characters wide, and contains an appropriate number of spaces followed by the grade-point average showing only the first two decimal places. This, if a person’s GPA is recorded as 2.714 then the field will be " 2.71". (Note that, with the space and the decimal point, the total number of characters is 5, as required.)

    A typical example of use is as follows:

    m111Report(c(2, 10, 15))
    ## male      about right     2.50
    ## female    overweight        NA
    ## male      underweight     3.20

    Note that you will have to re-code the feelings about weight.