Q1
Jacks.Equation <- function(a, b, c) {
return(a * b - c * 324 + log(a))
}
Jacks.Equation(a = 1000, b = 30, c = 7)
## [1] 27738.91
Q2
standardize.me <- function(x) {
return((x - mean(x)) / sd(x))
}
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
Q3
how.many <- function(data, value) {
result <- sum(data == value)
return(result)
}
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
Q4
Q6
madlib <- function(adjective, name, plural.noun) {
output <- paste("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", sep = "")
return(output)
}
madlib("hipster", "Bruce", "kale")
## [1] "If you talk to anhipsterpirate likeBruceyou may find that he/she spends more time talking aboutkalethan the pirate arts"
Q7
remove.outliers <- function(x) {
out.log <- x > (mean(x) + 2 * sd(x)) | x < (mean(x) - 2 * sd(x))
return(x[out.log == FALSE])
}
data <- c(rep(1, 50), -529484903)
remove.outliers(data)
## [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## [36] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Q8
ttest.apa <- function(x, null, p.critical) {
test.result <- t.test(x, mu = null)
test.statistic <- round(test.result$statistic, 2)
df <- round(test.result$parameter, 2)
p.value <- round(test.result$p.value, 2)
}
data <- seq(-10, 10, 1)
ttest.apa(x = data, null = 0, p.critical = .05)