Question (1) Write a function that satisfies the following conditions:
(a) The function takes a vector as the only input,
(b) The function returns a summary (mean, 5-number summaries, and
standard deviation) of the data, if the input is numeric, and
(c) The function returns a frequency table, if the input is a character vector.
Test your function using the following vectors:
x = c(12, 20, 18, 32, 15, 20)
y = c(“M”, “M”, “F”, “F”, “M”, “M”, “M”, “F”)
x = c(12, 20, 18, 32, 15, 20)
y = c("M", "M", "F", "F", "M", "M", "M", "F")
summary(x)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 12.00 15.75 19.00 19.50 20.00 32.00
summary(y)
## Length Class Mode
## 8 character character
Question (2) Write a function that satisfies the following conditions:
(a) The function takes a character vector as the only input, and
(b) The function returns a character vector such that it is the same as
the input vector except that each element of it only has the initial
letter in upper case.
Test your function using the dataset “mtcars” in base R. (in the console, type mtcars and hit the enter key.)
Hint: The built-in R functions that might be helpful are names(), substr(), toupper(), tolower(), paste0(), and sapply().
toupper(substr(names(mtcars),1,1))
## [1] "M" "C" "D" "H" "D" "W" "Q" "V" "A" "G" "C"
Note that if the character vector was not already all capitalized we could use the function toupper to satify the last condition mentions in part (b).