This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

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 meters that contains 30 trees when Jack is at a 7 on a drunkenness scale.
Jacks.gold <- function(area,trees,drunkness) {
output <- (area * trees) - drunkness*324 + log(area) 
return(output)
}
Jacks.gold(area = 1000, trees = 30, drunkness = 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)
  
}
Try your function on this vector, you should get the following result:
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 how.many that takes two arguments (data and value). The function should return a value indicating how many times the element value occurred in the vector data
how.many <- function(data, value) {
  
  output <- table(data == value) 
  
  return(output)
  
} 
Try running your function with these arguments:
how.many(data = c(1, 1, 9, 3, 2, 1, 1), value = 1)
## 
## FALSE  TRUE 
##     3     4
how.many(data = c(1, 1, 9, 3, 2, 1, 1), value = -100)
## 
## FALSE 
##     7
Question 4. Often times you will need to recode values of a dataset. For example, if you have a survey of age data, you may want to convert any crazy values (like anything below 0 or above 100) to NA. Let’s create a function to do this in R. Write a function called recode.numeric() with 3 arguments: x, lb, and ub. We’ll assume that x is a numeric vector. The function should look at the values of x, convert any values below lb and above ub to NA, and then return the resulting vector.
recode.numeric <- function(x, lb, ub) {
  
(outliers <- x > ub | x < lb)
  x[outliers] <- NA

return(x) 
}
Here is the function in action:
recode.numeric(x = c(5, 6, -10, 2, 1000, 2), lb = 0, ub = 100)
## [1]  5  6 NA  2 NA  2
Here is a dataset containing results from a survey containing 3 questions. The answer to every question should be between 1 and 10. Create a new dataframe called “survey.fixed” that contains the original data but with all invalid values converted to NA.
survey <- data.frame(
                     id = 1:6,
                     q1 = c(6, 2, 5, -1, 11, 100),
                     q2 = c(-5, 4, 65, 3, 7, 6),
                     q3 = c(2, 1, 2, 45, 5, -5)
                     )
####copy the dataframe:
survey.fixed <- data.frame(
                     id = 1:6,
                     q1 = c(6, 2, 5, -1, 11, 100),
                     q2 = c(-5, 4, 65, 3, 7, 6),
                     q3 = c(2, 1, 2, 45, 5, -5)
                     )
survey.fixed$id<- recode.numeric(survey$id, lb = 1, ub = 10)
survey.fixed$q1<- recode.numeric(survey$q1, lb = 1, ub = 10)
survey.fixed$q2<- recode.numeric(survey$q2, lb = 1, ub = 10)
survey.fixed$q3<- recode.numeric(survey$q3, lb = 1, ub = 10)

survey.fixed
##   id q1 q2 q3
## 1  1  6 NA  2
## 2  2  2  4  1
## 3  3  5 NA  2
## 4  4 NA  3 NA
## 5  5 NA  7  5
## 6  6 NA  6 NA
Question 5. Now, write recode.factor() which does the same thing as recode.numeric(), except with categorical (e.g.; factor) values. recode.factor() should have three arguments: x, old, and new. old is the vector of old values that will be replaced, and new is the vector of replacement values. For now, assume that the length of both old and new are always 3.
Here is the function in action. Let’s say you’re throwing a party for a group of 8 young pirates. You ask each one what they would like to drink and store the results in a vector called orders. Here is the vector
orders <- c("coke light", 
            "coke", 
            "pepsi", 
            "coke", 
            "coke light", 
            "water", 
            "pepsi", 
            "pepsi light", 
            "water", 
            "water")
Now, unfortunately your ship is sponsored by pepsi, so you don’t have any coke products. So you’ll need to replace all the coke products with pepsi products. For example, “coke” should become “pepsi”, and “coke light” should become “pepsi light”. Also, because you don’t want the party to be too lame, you want to replace all the orders for “water”" with “pepsi max”.
old <- c("coke", "coke light", "water")
new <- c("Pepsi", "Pepsi light", "Pepsi Max")

recode.factor <- function(x, old,new) {
  x[x == old[1]] <- new[1]
  x[x == old[2]] <- new[2]
  x[x == old[3]] <- new[3]
  
 return(x)
  
}
Now use your function to fix the orders!
recode.factor(orders, old = c("coke", "coke light", "water"),
new = c("Pepsi", "Pepsi light", "Pepsi Max") )
##  [1] "Pepsi light" "Pepsi"       "pepsi"       "Pepsi"       "Pepsi light"
##  [6] "Pepsi Max"   "pepsi"       "pepsi light" "Pepsi Max"   "Pepsi Max"
Question 6. 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 indicating 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, "than the pirate arts" )

  return (output)}
Try your function with the arguments “hipster”, “Bruce” and “kale”
madlib(adjective = "hipster", name = "Bruce", plural.noun = "kale")
## [1] "If you talk to an hipster pirate like Bruce you may find that he/she spends more time talking about kale than the pirate arts"