#Question 1. 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,b,c) {
output <- ((a * b) - c * 324 + log(a))
return(output)
}
Jacks.Equation(a=1000, b=30, c=7)
## [1] 27738.91
#Jack will find 27738.91 units gold!
#Question 2. 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).
standardize.me <- function(x) {
output <- (x-mean(x))/sd(x)
return(output)
}
data <- c(6, 3, 8, 6, 3, 2, 3, 2, 100)
standardize.me(data)
## [1] -0.2740789 -0.3677514 -0.2116305 -0.2740789 -0.3677514 -0.3989756
## [7] -0.3677514 -0.3989756 2.6609937
#Question 3. 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.
remove.outliers <- function(x){
is.outliers <- x < (mean(x)-(2*sd(x))) | x > (mean(x)-(2*sd(x)))
new.vec <- x[is.outliers == FALSE]
output <- new.vec
return(output)
}
data <- c(rep(1, 100), -529484903)
mean(data)
## [1] -5242424
mean(remove.outliers(data))
## [1] NaN
#Question 4. 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
how.many <- function(data,value) {
log.vec <- data == value
n.true <- sum(log.vec)
return(n.true)
}
how.many(data = c(1, 1, 9, 3, 2, 1, 1), value = 1)
## [1] 4
#4
how.many(data = c(1, 1, 9, 3, 2, 1, 1), value = -100)
## [1] 0
#0
#Question 5. 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
#Version1
a <- "adjective"
b <- "name"
c <- "pluralnoun"
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.")
## [1] "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."
#[1] "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."
#Version2
madlib <- function(a,b,c) {
output <- 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.")
return(output)
}
madlib("ADJECTIVE", "NAME", "PLURAL NOUN")
## [1] "If you talk to an ADJECTIVE pirate like NAME you may find that he/she spends more time talking about PLURAL NOUN than the pirate arts."
# [1] "If you talk to an ADJECTIVE pirate like NAME you may find that he/she spends more time talking about PLURAL NOUN than the pirate arts."
#Question 6. 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
#ttest.apa <- function(x, null, p.critical) {
#t.test(x)
#p.critical <- .05
#if (p.critical < .05) {print ("A one sample t-test was significant")}
#}
#data <- c(5 ,7, 6, 4, 8, 7, 5, 6, 5)
#ttest.apa(x = data, null = 0, p.critical = .05)