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)
}
```

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. => 27738.91

`Jacks.Equation (a= 1000, b= 30, c= 7)`

`## [1] 27738.91`

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.

When you are finished writing the function, run these commands to make sure your function works

```
remove.outliers <- function (x) {
is.outlier <- x< mean (x) -2*sd(x) | x> mean (x) + 2*sd (x)
new.vector <- x[!is.outlier]
return (new.vector)
}
data <- c(rep(1, 100), -529484903)
mean(data)
```

`## [1] -5242424`

`mean(remove.outliers(data))`

`## [1] 1`

-5242424, 1

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) {
value.log <- data == value
output <- sum (value.log)
return (output)
}
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`

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

```
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)
return (output)
}
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"`

`madlib("inattractive", "my supervisor", "boring stuff")`

`## [1] "If you talk to an inattractive pirate like my supervisor you may find that he/she spends more time talking about boring stuff"`

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

unfinished?

ttest.apa <- function (x, null, p.critical){

test.tesult <- t.test(x, mu=0, alternative = ?t?)

if (test.tesults\(parameter < test.tesults\)p.value) {

output <- paste(" (t(" ,test.result\(parameter , ")=", test.result\)statistic, ?p=?, test.result$p.value)

if (test.tesults\(parameter > test.tesults\)p.value) {print (?A one sample t-test was significant?)}

output <- paste(" (t(" ,test.result\(parameter , ")=", test.result\)statistic, ?p=?, test.result$p.value)

return (output)

}

Copyright © 2016 thetazero.com All Rights Reserved. Privacy Policy