# Functions

Author

Jeffrey R. Stevens

Published

March 27, 2023

1. Write a function called `mystring` that takes a vector as an argument and returns the first three characters from the string. Test it on `words[1:10]`.
``````library(stringr)
mystring <- function(x) {
str_sub(x, 1, 3)
}
mystring(words[1:10])``````
`` [1] "a"   "abl" "abo" "abs" "acc" "acc" "ach" "acr" "act" "act"``
1. Add an argument to `mystring()` that allows the user to control how many of the first characters should be returned. Test it on `words[1:10]` with 5 characters.
``````mystring <- function(x, chars) {
str_sub(x, 1, chars)
}
mystring(words[1:10], chars = 5)``````
`````` [1] "a"     "able"  "about" "absol" "accep" "accou" "achie" "acros" "act"
[10] "activ"``````
1. Set the default number of characters returned by `mystring()` to be 3 and test that the default works and that you can override the default.
``````mystring <- function(x, chars = 3) {
str_sub(x, 1, chars)
}
mystring(words[1:10])``````
`` [1] "a"   "abl" "abo" "abs" "acc" "acc" "ach" "acr" "act" "act"``
``mystring(words[1:10], chars = 5)``
`````` [1] "a"     "able"  "about" "absol" "accep" "accou" "achie" "acros" "act"
[10] "activ"``````
1. Add a step that checks whether the inputted vector is a character string. If it is, continue to return the truncated strings. If the vector is not a character string, use the `stop()` function to stop the computation and return a message to the console telling the user that the vector was not a character vector. Test your function with a character vector, a numeric vector, and a logical vector.
``````mystring <- function(x, chars = 3) {
if (!is.character(x)) {
} else {
str_sub(x, 1, chars)
}
}

mystring(words[1:10])``````
`` [1] "a"   "abl" "abo" "abs" "acc" "acc" "ach" "acr" "act" "act"``
``````# mystring(1:10)
# mystring(c(TRUE, FALSE))``````
1. Create a function called `parse_my_vector` that does the following:
• Allows users to input a vector and a response to the argument `type` that determines whether the vector is a numeric (`"num"`), character (`"char"`), or logical (`"logical"`) vector. There should be no default value. If the user response does not match any of these three strings, stop with a message asking the user to specify one of the three strings.
• For each type, checks whether the vector is actually the type specified by the user and stops with a message if they do not match.
• For numeric vectors, multiplies by 10. For character vectors, extracts the first three characters. For logical vectors, returns the number of TRUE responses.
• Before returning output, prints a message thanking the user.
• Returns the original vector and output of the functions described above.
``````parse_my_vector <- function(x, type) {
if (type == "num") {
if (is.numeric(x)) {
output <- x / 10
} else {
stop("Type response does not match vector type.")
}
} else if (type == "char") {
if (is.character(x)) {
output <- str_sub(x, 1, 3)
} else {
stop("Type response does not match vector type.")
}
} else if (type == "logical") {
output <- sum(x)
} else {
stop("Please enter either 'num', 'char', or 'logical'.")
}
message("Thank you!")
list(x,output)
}``````
1. Check the following with `parse_my_vector()`:
• x = 1:10, type = “num”
• x = 1:10, type = “char”
• x = words[1:10], type = “num”
• x = words[1:10], type = “char”
• x = c(TRUE, FALSE, TRUE), type = “num”
• x = c(TRUE, FALSE, TRUE), type = “logical”
• x = 1:10, type = “nums”
``parse_my_vector(x = 1:10, type = "num")``
``Thank you!``
``````[[1]]
[1]  1  2  3  4  5  6  7  8  9 10

[[2]]
[1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0``````
``````# parse_my_vector(x = 1:10, type = "char")
# parse_my_vector(x = words[1:10], type = "num")
parse_my_vector(x = words[1:10], type = "char")``````
``Thank you!``
``````[[1]]
[1] "a"        "able"     "about"    "absolute" "accept"   "account"
[7] "achieve"  "across"   "act"      "active"

[[2]]
[1] "a"   "abl" "abo" "abs" "acc" "acc" "ach" "acr" "act" "act"``````
``````# parse_my_vector(x = c(TRUE, FALSE, TRUE), type = "num")
parse_my_vector(x = c(TRUE, FALSE, TRUE), type = "logical")``````
``Thank you!``
``````[[1]]
[1]  TRUE FALSE  TRUE

[[2]]
[1] 2``````
``# parse_my_vector(x = 1:10, type = "nums")``