- Captain Jack is convinced that he can predict how much gold he will find on an island with the following equation: (a * b) - c * 324 + log(a), where a is the area of the island in square meters, b is the number of trees on the island, and c is how drunk he is on a scale of 1 to 10. Create a function called Jacks.Equation that takes a, b, and c as arguments and returns Captain Jack’s predictions. Test your function for an island with an area of 1,000 square meeters that contains 30 trees when Jack is at a 7 / 10 on a drunkenness scale.
Jacks.Equation <- function(a = 1000, b = 30, c = 7) {
output <- (a * b) - c * 324 + log(a)
return(output)
}
Jacks.Equation ()
## [1] 27738.91
- Write a function called standardize.me that takes a vector x as an argument, and returns a vector that standardizes the values of x (standardization means subtracting the mean and dividing by the standard deviation). Try your function on this vector:
data <- c(6, 3, 8, 6, 3, 2, 3, 2, 100)
standardize.me <- function(x)
{
output <- (x- mean(x)) / sd(x)
return(output)
}
standardize.me
## function(x)
## {
## output <- (x- mean(x)) / sd(x)
## return(output)
## }
- Write a function called remove.outliers that takes a vector as an argument, determines which values of the vector are outliers, and returns a vector with the outliers removed. Define an outlier as any value that is less than 2 standard deviations below the mean, or more than 2 standard deviations above the mean. When you are finished writing the function, run these commands to make sure your function works.
remove.outliers <- function(x) {
is.outlier.log <- x > (mean(x) + 2 * sd(x)) | x < (mean(x) - 2 * sd(x))
new.vec <- x [is.outlier.log == FALSE]
return(new.vec)}
remove.outliers (data)
## [1] 6 3 8 6 3 2 3 2
data <- c(rep(1, 100), -529484903)
mean(data)
## [1] -5242424
mean(remove.outliers(data))
## [1] 1
- Write a function called how.many that takes two arguments (data and value). The function should return a value indicating how many times the element value occured in the vector data Try running your function with these arguments:
how.many <- function(data, value)
{
log.how.many <- data == value
new.vec <- data [log.how.many]
return (length(new.vec))
}
how.many(data = c(1, 1, 9, 3, 2, 1, 1), value = 1)
## [1] 4
how.many(data = c(1, 1, 9, 3, 2, 1, 1), value = -100)
## [1] 0
- Write a function called madlib that takes three strings as arguments, and returns the following sentence with the string arguments inserted into the following text: “If you talk to an ADJECTIVE pirate like NAME you may find that he/she spends more time talking about PLURALNOUN than the pirate arts.” Your three arguments to the function should be: • adjective, a string indicating an adjective • name, a string of a person’s name • plural.noun, a string indicatinga plural noun For example, if you run your function with the arguments “ADJECTIVE”, “NOUN” and “PLURAL NOUN”, your function should return the following values. madlib(“ADJECTIVE”, “NOUN”, “PLURAL NOUN”) ## [1] “If you talk to an ADJECTIVE pirate like NOUN, you may find that he/she spends more time talking about PLURAL NOUN than the pirate arts.” When you have written your function, execute the function with your own values of the inputs.
madlib <- function(a, b, c)
{
paste("If you talk to an",a, "pirate like", b, "you may find that he/she spends more time talking about", c, "than the pirate arts.") }
madlib ("Adjective","Noun", "Plural Noun")
## [1] "If you talk to an Adjective pirate like Noun you may find that he/she spends more time talking about Plural Noun than the pirate arts."
- Write a function called ttest.apa that takes a numeric vector as an argument, conducts a one-sample t.test on that vector, and returns a string summarising the test in APA style. Your function should have 3 arguments • x, a vector of data • null, the population mean under the null hypothesis • p.critical, the critical value for determining significance Once you have programmed your function, test it on these data:
ttest.apa <- function(x, mean, p.critical)
{ t.test(x, null, p.critical)}