library(tidyverse)
library(stringr)
# Creating a function called mySum with 2 required arguments
mySum <- function(number1, number2) {
return(number1 + number2)
}
# Defaulting to order of arguments in function
mySum(5, 3)
## [1] 8
# Manually specifying arguments you can than switch what order you put the arguments
mySum(number1 = 5, number2 = 3)
## [1] 8
mySum(13, 1989)
## [1] 2002
mySum(NA, 1989)
## [1] NA
# Creating a function called mySum with 2 required arguments, 1 optional argument
myNewSum <- function(number1, number2, remove.na = TRUE) {
if(remove.na) {
#If remove.na is true the ifelse statement replaces NA with o
calcSum <- ifelse(is.na(number1), 0, number1) +
ifelse(is.na(number2), 0, number2)
} else {
#otherwise, add the two numbers
calcSum <- number1 + number2
}
return(calcSum)
}
myNewSum(NA,10)
## [1] 10
What do these functions do?
#checks if string starts with a giving prefix
checkPrefix <- function(string, prefix) {
str_sub(string, 1, nchar(prefix)) == prefix
}
#Create a function to drop last value of vector
dropLastValue <- function(x) {
#return null if single value
if (length(x) <= 1) return(NULL)
#drop last value of x otherwise
x[-length(x)]
}
#Create function to calculate the harmonic mean
harmonicMean <- function(x) {
1 / (mean(1 / x))
}
#function to calculate the geometric mean x should be a vector
GeometricMean <- function(x) {
return(prod(x)^(1/length(x)))
}
GeometricMean(x = c(3,4,5,6))
## [1] 4.355877
# Function to calculate square root
calcSqrt <- function(val) {
if (val >= 0) {
return(val^0.5)
} else {
warning("val is negative but must be >= 0!")
return(NaN)
}
}
calcSqrt(4)
## [1] 2
## [1] 2
calcSqrt(-9)
## Warning in calcSqrt(-9): val is negative but must be >= 0!
## [1] NaN
## [1] NaN
foodType <- function(food) {
if (food %in% c("apple", "orange", "banana")) {
return("fruit")
} else if(food %in% c("broccoli", "asparagus")) {
return("vegetable")
} else {
return("other")
}}
foodType("Reese's Puffs")
## [1] "other"
## [1] "other"
foodType("apple")
## [1] "fruit"
## [1] "fruit"
Is it a whole number?
is.whole.number <- function(x) {roundedx <- round(x)
return(x == roundedx)}
is.whole.number(6)
## [1] TRUE
raise_power <-function(x, power) {
if (x< 0 | is.whole.number(power) == FALSE) {warning("that number is negative it needs to be positive or the power is not a whole number")
return(NaN)
} else (
return(x^power)
)}
raise_power(4,2)
## [1] 16
raise_power(-9,2)
## Warning in raise_power(-9, 2): that number is negative it needs to be positive
## or the power is not a whole number
## [1] NaN
raise_power(3,1/3)
## Warning in raise_power(3, 1/3): that number is negative it needs to be positive
## or the power is not a whole number
## [1] NaN
raise_power(-3,1/3)
## Warning in raise_power(-3, 1/3): that number is negative it needs to be positive
## or the power is not a whole number
## [1] NaN
# Creating a function called mySum with 2 required arguments, 1 optional argument
myNewSum <- function(number1, number2, remove.na = TRUE) {
if(remove.na) {
calcSum <- ifelse(is.na(number1), 0, number1) +
ifelse(is.na(number2), 0, number2)
} else {
calcSum <- number1 + number2
}
return(calcSum)
}
# The 'equals' operator
3 == 5
## [1] FALSE
(4/2) == 2
## [1] TRUE
3 != 5
## [1] TRUE
# Vectorized examples
1:4
## [1] 1 2 3 4
1:4 == 3
## [1] FALSE FALSE TRUE FALSE
(1:4 < 4) & (1:4 > 1)
## [1] FALSE TRUE TRUE FALSE
(1:4 < 4) | (1:4 > 1)
## [1] TRUE TRUE TRUE TRUE
#Create a function that takes a numeric vector as input, and returns a single boolean value indicating whether or not the vector has any negative values.
any_negatives <- function(vector){
any(vector<0)
}
any_negatives(c(1,2,-3))
## [1] TRUE
#looks ar all values in both vectors
(1:4 < 4) & (1:4 > 1)
## [1] FALSE TRUE TRUE FALSE
#looks at only the first number in the vector
(1:4 < 4) && (1:4 > 1)
## Warning in (1:4 < 4) && (1:4 > 1): 'length(x) = 4 > 1' in coercion to
## 'logical(1)'
## Warning in (1:4 < 4) && (1:4 > 1): 'length(x) = 4 > 1' in coercion to
## 'logical(1)'
## [1] FALSE