Exercise One

Write a function what should return the sum of 3 integers.

#Write function 
sum_three <- function(int1,int2,int3){
  int1 + int2 + int3
} 

#Test function 
int1 <- 5
int2 <- 4
int3 <- 2
test_sum_three <- sum_three(int1,int2,int3)

The result of inputting 5, 4, 2 into my sum_three function is 11

Exercise Two

Write a function what should return TRUE if a given integer is inside a vector.

# Create vector of integers 
e2_vector <- c(1,5,19,7,5,22,41,15)

#Write function 
logic_int_test <- function(test_value){
  test_value %in% e2_vector
} 

#Test function 
logic_int1 <- 5
logic_int2 <- 18
logic_int3 <- 41

test_logic_int_test1 <- logic_int_test(logic_int1)
test_logic_int_test2 <- logic_int_test(logic_int2)
test_logic_int_test3 <- logic_int_test(logic_int3)

test_logic_int_test1
## [1] TRUE
test_logic_int_test2
## [1] FALSE
test_logic_int_test3
## [1] TRUE

As we can see, when I tested logic_int1, (5) and logic_int3, (41) they were clearly part of the e2_vector. But when I tested logic_int2, (18) FALSE was returned.

Exercise Three

Create a function given a data frame will print by screen the name of the column and the class of data it contains (e.g. Variable 1 is Numeric).

#Create a data frame 
Character <- c("Hello","World","Please","Reply")
Numeric <- c(1.4,45.0,14,0.5)
Logical <- c(TRUE,FALSE,TRUE,TRUE)
e3_df <- data.frame(Character,Numeric,Logical)

#Print DF and test columns are unique classes 
e3_df
##   Character Numeric Logical
## 1     Hello     1.4    TRUE
## 2     World    45.0   FALSE
## 3    Please    14.0    TRUE
## 4     Reply     0.5    TRUE
str(e3_df)
## 'data.frame':    4 obs. of  3 variables:
##  $ Character: chr  "Hello" "World" "Please" "Reply"
##  $ Numeric  : num  1.4 45 14 0.5
##  $ Logical  : logi  TRUE FALSE TRUE TRUE
#Create a function as requested 
test_col_class <- function(col){
  if (col > length(e3_df)) {
    print("You must enter a number 1 through 3")}
  else {class(e3_df[,col])}
}

#Test 4 scenario's 
test_col_class(1)
## [1] "character"
test_col_class(2)
## [1] "numeric"
test_col_class(3)
## [1] "logical"
test_col_class(4)
## [1] "You must enter a number 1 through 3"

As we can see, when I tested test_col_class with numbers 1-3, it returned the high level class of the column within my e3_df data frame. When I exceeded the number of columns (where col > length(e3_df)) my function printed direction on the inputs we would accept.

Exercise Four

Create a function given a vector will return a new vector with the elements of the first vector with duplicated elements removed.

# Create a vector with duplicate elements 
e4_vector <- c(12,15,14,15,11,80,45,16,15,12,9,9)

# Our function must take in a vector, run through each element and check if it's anywhere else. If so, delete it. 
e4_function <- function(vec){
  for (i in 1:length(vec)){
    if (vec[i] %in% vec[-i]){
      vec <- vec[-i]
      print(vec)
      i <- i + 1}
  }
  return(vec)
}
e4_function(e4_vector)
##  [1] 15 14 15 11 80 45 16 15 12  9  9
##  [1] 15 14 11 80 45 16 15 12  9  9
## [1] 15 14 11 80 45 16 12  9  9
## [1] 15 14 11 80 45 16 12  9
## [1] 15 14 11 80 45 16 12  9
# Test function with recommended input 
e4_function(c(1,2,3,3,3,4))
## [1] 1 2 3 3 4
## [1] 1 2 3 4
## [1] 1 2 3 4

Exercise Five

Create a function given a vector and an integer will return how many times the integer appears inside the vector.

# Create a vector with duplicate elements 
e4_vector <- c(12,15,14,15,11,80,45,16,15,12,9,9)
s<-0
# Our function must take in a vector, run through each element and check if it's there. If so, count it. 
e5_function <- function(vec,int){
  for (i in 1:length(vec)){
    if (int %in% vec[i]){
      s <- s + 1}
  }
  return(s)
}
# Test the number 15 
e5_function(e4_vector,15)
## [1] 3
# Test the number 9
e5_function(e4_vector,9)
## [1] 2
# Test the number 5
e5_function(e4_vector,5)
## [1] 0
# Test function with recommended input 
e5_function(c(1,2,3,3,3,4),3)
## [1] 3

Exercise Six

Create a function given a vector will print by screen the mean and the standard deviation, it will optionally also print the median.

# Create a vector with duplicate elements 
e4_vector <- c(12,15,14,15,11,80,45,16,15,12,9,9)
new_sum_cycle<-0
# Our function must take in a vector, and print the mean, sd, and median, all manually 
e6_function <- function(vec){
  
  #Calculate Mean 
  mean_func <- round((sum(vec)/length(vec)),3)
  print(paste("This vector's mean is ",mean_func))

  
  #Calculate Standard Deviation 
  for(i in length(vec)){
    sum_cycle <- (vec[i] - mean_func)^2
    new_sum_cycle <- new_sum_cycle + sum_cycle
  }
  sd_func <- round((sqrt((1/length(vec)) * new_sum_cycle)),3)
  print(paste("This vector's standard deviation is ",sd_func))
  
  #Calculate Median 
  vec <- sort(vec, decreasing = FALSE)
  if(((length(vec)/2) %% 1) == 0){
    middle_left <- vec[(length(vec)/2 + 0.5)]
    middle_right <- vec[(length(vec)/2 + 1.5)]
    median_func <- (middle_left+middle_right) / 2
  }
  else{
    sort(vec, decreasing = FALSE)
    median_func <- round(((length(vec)/2)+0.5),3)
  }
  print(paste("This vector's median is ",median_func))
}

# Test the function
e6_function(e4_vector)
## [1] "This vector's mean is  21.083"
## [1] "This vector's standard deviation is  3.488"
## [1] "This vector's median is  14.5"

Exercise Seven

Create a function given an integer will calculate how many divisors it has (other than 1 and itself). Make the divisors appear by screen.

e7_function <- function(int){
  s <- 0
  for(i in 1:int){
    if(i != 1 & i != int){
      if(int %% i == 0){
        print(paste("Your number ",int," is divisible by ", i))
        s <- s+1
      }
    }
  }
  print("All of the above, in addition to the number 1 and itself")
  print(paste("Number of divisors are",s +2))
}
#test our function
e7_function(15)
## [1] "Your number  15  is divisible by  3"
## [1] "Your number  15  is divisible by  5"
## [1] "All of the above, in addition to the number 1 and itself"
## [1] "Number of divisors are 4"
e7_function(65)
## [1] "Your number  65  is divisible by  5"
## [1] "Your number  65  is divisible by  13"
## [1] "All of the above, in addition to the number 1 and itself"
## [1] "Number of divisors are 4"
e7_function(4272)
## [1] "Your number  4272  is divisible by  2"
## [1] "Your number  4272  is divisible by  3"
## [1] "Your number  4272  is divisible by  4"
## [1] "Your number  4272  is divisible by  6"
## [1] "Your number  4272  is divisible by  8"
## [1] "Your number  4272  is divisible by  12"
## [1] "Your number  4272  is divisible by  16"
## [1] "Your number  4272  is divisible by  24"
## [1] "Your number  4272  is divisible by  48"
## [1] "Your number  4272  is divisible by  89"
## [1] "Your number  4272  is divisible by  178"
## [1] "Your number  4272  is divisible by  267"
## [1] "Your number  4272  is divisible by  356"
## [1] "Your number  4272  is divisible by  534"
## [1] "Your number  4272  is divisible by  712"
## [1] "Your number  4272  is divisible by  1068"
## [1] "Your number  4272  is divisible by  1424"
## [1] "Your number  4272  is divisible by  2136"
## [1] "All of the above, in addition to the number 1 and itself"
## [1] "Number of divisors are 20"
# Testing a prime number yields the desired result, nothing prints since the only divisor is 1 and itself
e7_function(4273)
## [1] "All of the above, in addition to the number 1 and itself"
## [1] "Number of divisors are 2"
# Test function with recommended input 
e7_function(12)
## [1] "Your number  12  is divisible by  2"
## [1] "Your number  12  is divisible by  3"
## [1] "Your number  12  is divisible by  4"
## [1] "Your number  12  is divisible by  6"
## [1] "All of the above, in addition to the number 1 and itself"
## [1] "Number of divisors are 6"

Exercise Eight

Create a function given a data frame, and a number or character will return the data frame with the character or number changed to NA.

e8_funtion <- function(dfinput){
  if(length(which(e3_df==dfinput))){
    e3_df[e3_df == dfinput] <- "NA"
    #e3_df <- replace(e3_df,e3_df[dfinput],"NA")
    print(e3_df)
  }
  else{print("your specified search term is not included in our Data Frame e3_df")}
}
#Test known character
e8_funtion("Hello")
##   Character Numeric Logical
## 1        NA     1.4    TRUE
## 2     World    45.0   FALSE
## 3    Please    14.0    TRUE
## 4     Reply     0.5    TRUE
#Test known numeric
e8_funtion(1.4)
##   Character Numeric Logical
## 1     Hello      NA    TRUE
## 2     World      45   FALSE
## 3    Please      14    TRUE
## 4     Reply     0.5    TRUE
#Test known variable not included
e8_funtion(55)
## [1] "your specified search term is not included in our Data Frame e3_df"