R/professor_info_functions.R

#' Calculates the age of a professor
#' @param person A professor
#' @return age Assuming the age of receipt is 22, the current age of the person
#' @export
calculate_age <- function(person){
  if(is.integer0(grep(",", person))){ #there is no coma means invalid list
    return(0)
  }
  year <- get_prof_year(person)
  arr <- split_trim(person)
  ba <- grep(ba_pattern(), sanitize(arr))
  if(length(ba) == 0){ # BA was not found
    return(0)
  }else{
    rest <- arr[ba[[1]]:length(arr)]
    years <- grep("[0-9]{4}", rest) #look for the first year that occurs after the BA is found
    ba_year <- rest[years[1]] #peel off the first year
    return(year - as.integer(ba_year) + 22)
  }
}

#' Determines whether a person has a degree of a given type
#' @param person The information for a faculty member
#' @param degree_type either "BA", "MA", or "PHD"
#' @return year 0 if the degree was not found or the year of the first degree of the desired type earned
#' @export
has_degree <- function(person, degree_type){
  arr <- split_trim(person)
  if(degree_type == "BA"){
    pattern = ba_pattern()
  } else if(degree_type == "MA"){
    pattern = ma_pattern()
  } else if(degree_type == "PHD"){
    pattern = phd_pattern()
  } else {
    pattern = NA
  }
  degree <- grep(pattern, sanitize(arr))
  if(length(degree) == 0){
    return(0)
  } else {
    rest <- arr[degree[[1]]:length(arr)] #check out the first time the degree was recieved
    years <- grep("[0-9]{4}", rest)
    return(as.integer(rest[years[1]]))
  }
}

#' Finds the appropriate abbreviations for each type of degree
#' @param degree_type For functionality, needs to be one of "PHD", "MA", or "BA".
#' @return valid_degrees A list of degrees of equivalent educational level
find_degrees <- function(degree_type){
  if(degree_type == "PHD"){
    valid_degrees = phd_degrees
  } else if(degree_type == "MA"){
    valid_degrees = ma_degrees
  } else if(degree_type == "BA"){
    valid_degrees = ba_degrees
  }
  return(valid_degrees)
}

#' Removes all non-digit characters from a string
#' @param str String to convert to integer
#' @return int The converted String
# @example
# str = "Rutgers 2007"
# 2007 == smart_convert(str)
smart_convert <- function(str){
  as.integer(gsub("\\D+", "", str))
}
PhilBrockman/WilliamsStaff documentation built on May 8, 2019, 1:33 a.m.