Defines functions changeTypeOfColumns

Documented in changeTypeOfColumns

#' Change the data type of the columns in data frame
#' Modify the data type of specified columns. Can coerce the column to the type specified by user.
#' @param df A data frame for which we wish to change the data type of the columns
#' @param rangeOfColumns A vector of natural numbers specifyung which columns to coerce.
#' @param type One of 'numeric', 'character', 'factor'
#' @author Alexander Kheirallah ([email protected])
#' @export

changeTypeOfColumns <- function(df, rangeOfColumns, type){
  if(class(rangeOfColumns) != "integer") stop("rangeOfColumns paramter is not a sequence of natural numbers. Exiting.")
  if(!(type %in% c("numeric","character","factor"))) stop("data type specification is not one of 'numeric', 'character', or 'factor'")
  for( i in rangeOfColumns) {
    if(type == "numeric"){
      df[,i] <- as.numeric(df[,i])
    if(type == "character"){
      df[,i] <- as.character(df[,i])
    if(type == "factor"){
      df[,i] <- as.factor(df[,i])
msxakk89/dat documentation built on March 25, 2020, 3:18 p.m.