R/merge_and_drop.R

Defines functions merge_and_drop

Documented in merge_and_drop

#' This function perfoms the merge-and-drop technique for condensing the metadata dataframe as discussed at a meeting. Shifts from col2 to col1.
#' @import rvest
#' @import dplyr
#' @import magrittr
#' @import stringr
#' @import purrr
#' @param df, data frame
#' @param col1, the first column
#' @param col2, the second column
#' @return merged dataframe
#' @export

merge_and_drop <- function(df, col1, col2) {
  # Handle if one column does not exist
  tryCatch(expr = {
    df[col1]
  }, error = function(error) {
    return(df)
  })
  tryCatch(expr = {
    df[col2]
  }, error = function(error) {
    return(df)
  })
  for(i in 1:nrow(df)) {
    if(!is.na(df[i, col2]) & is.na(df[i, col1])) {
      df[i, col1] <- df[i, col2]
    }
  }
  if(col1 != col2) {
    df <- df[names(df) != col2]
  }
  return(df)
}
kingsuching/Frost2021Package documentation built on March 19, 2022, 11:51 p.m.