R/convert_variables.R

#' @title Convert the Data Types of Variables from a given Dataset
#' 
#' @description Convert the Data Types of Variables from a given Dataset
#' 
#' @param dataset A dataset from which the variables are converted
#' 
#' 
#' @param file_name A character object indicating the file name when saving the data frame.
#'                  The default is NULL.
#'                  The name must include the .csv suffixs.
#'
#' @param directory A character object specifying the directory where the data frame is to be saved as a .csv file.
#'
#' 
#' @return Outputs the descriptive statistics as a data frame.
#' 
#' @export
#' 
#' @seealso 
#'
convert_variables <- function(dataset, 
                              convert.from = c(NULL, "double", "numeric", "integer", "character", "factor"), 
                              convert.to = c("double", "numeric", "integer", "character", "factor"), 
                              file_name = NULL, 
                              directory = NULL)
  {
  
  # Make sure the datset is converted to a data frame
  dataset <- as.data.frame(dataset)
  
  # Confirm correct choice for convert.from
  convert.from <- match.arg(convert.from)
  
  # Confirm correct choice for convert.to
  convert.to <- match.arg(convert.to)
  
  #---------------------------------------------------------------------------#
  # If convert.from is NULL                                                   #
  #---------------------------------------------------------------------------#
  
  if(is.null(convert.from) & convert.to == "double"){
    
    for (i in 1:ncol(dataset)){
      
      # create empty dataframe to store the interaction terms 
      converted_data <- as.data.frame(matrix(nrow = nrow(dataset), 
                                             ncol = 1))
      
      # create c a column index for the converted data
      c = 1
      converted_data[,c] <- as.double(dataset[,i])
      
      # add in the column names to the dataset
      colnames(converted_data)[c] <- colnames(dataset)[i]
      
      # update column index c
      c = c + 1
      
    }
    
  } else if(is.null(convert.from) & convert.to == "numeric"){
    
    for (i in 1:ncol(dataset)){
      
      # create empty dataframe to store the interaction terms 
      converted_data <- as.data.frame(matrix(nrow = nrow(dataset), 
                                             ncol = 1))
      
      # create c a column index for the converted data
      c = 1
      converted_data[,c] <- as.numeric(dataset[,i])
      
      # add in the column names to the dataset
      colnames(converted_data)[c] <- colnames(dataset)[i]
      
      # update column index c
      c = c + 1
      
    }
    
  } else if(is.null(convert.from) & convert.to == "integer"){
    
    for(i in 1:ncol(dataset)){
      
      # create empty dataframe to store the interaction terms 
      converted_data <- as.data.frame(matrix(nrow = nrow(dataset), 
                                             ncol = 1))
      
      # create c a column index for the converted data
      c = 1
      converted_data[,c] <- as.integer(dataset[,i])
      
      # add in the column names to the dataset
      colnames(converted_data)[c] <- colnames(dataset)[i]
      
      # update column index c
      c = c + 1
      
    }
    
  } else if(is.null(convert.from) & convert.to == "character"){
    
    for(i in 1:ncol(dataset)){
      
      # create empty dataframe to store the interaction terms 
      converted_data <- as.data.frame(matrix(nrow = nrow(dataset), 
                                             ncol = 1))
      
      # create c a column index for the converted data
      c = 1
      converted_data[,c] <- as.character(dataset[,i])
      
      # add in the column names to the dataset
      colnames(converted_data)[c] <- colnames(dataset)[i]
      
      # update column index c
      c = c + 1
      
    }
    
  } else if(is.null(convert.from) & convert.to == "factor"){
    
    for (i in 1: ncol(dataset)){
      
      # create empty dataframe to store the interaction terms 
      converted_data <- as.data.frame(matrix(nrow = nrow(dataset), 
                                             ncol = 1))
      
      # create c a column index for the converted data
      c = 1
      converted_data[,c] <- as.factor(dataset[,i])
      
      # add in the column names to the dataset
      colnames(converted_data)[c] <- colnames(dataset)[i]
      
      # update column index c
      c = c + 1
      
    }
    
  #-------------------------------------------------------------------------------#
  # If convert.from is not NULL                                                   #
  #-------------------------------------------------------------------------------#
    
  } else if(convert.to == "double" & convert.to == "numeric"){
    
    for (i in 1:ncol(dataset)){
      
      if(is.double(dataset[,i])){
        
        # create empty dataframe to store the interaction terms 
        converted_data <- as.data.frame(matrix(nrow = nrow(dataset), 
                                               ncol = 1))
        
        # create c a column index for the converted data
        c = 1
        converted_data[,c] <- as.numeric(dataset[,i])
        
        # add in the column names to the dataset
        colnames(converted_data)[c] <- colnames(dataset)[i]
        
        # update column index c
        c = c + 1
        
      }
      
    }
    
  } else if(convert.to == "double" & convert.to == "integer"){
    
    for (i in 1:ncol(dataset)){
      
      if(is.double(dataset[,i])){
        
        # create empty dataframe to store the interaction terms 
        converted_data <- as.data.frame(matrix(nrow = nrow(dataset), 
                                               ncol = 1))
        
        # create c a column index for the converted data
        c = 1
        converted_data[,c] <- as.integer(dataset[,i])
        
        # add in the column names to the dataset
        colnames(converted_data)[c] <- colnames(dataset)[i]
        
        # update column index c
        c = c + 1
        
      }
      
    }
    
  } else if(convert.from == "double" & convert.to == "character"){
    
    for (i in 1:ncol(dataset)){
      
      if(is.double(dataset[,i])){
        
        # create empty dataframe to store the interaction terms 
        converted_data <- as.data.frame(matrix(nrow = nrow(dataset), 
                                               ncol = 1))
        
        # create c a column index for the converted data
        c = 1
        converted_data[,c] <- as.character(dataset[,i])
        
        # add in the column names to the dataset
        colnames(converted_data)[c] <- colnames(dataset)[i]
        
        # update column index c
        c = c + 1
        
      }
      
    }
    
  } else if(convert.from == "double" & convert.to == "factor"){
    
    for (i in 1:ncol(dataset)){
      
      if(is.double(dataset[,i])){
        
        # create empty dataframe to store the interaction terms 
        converted_data <- as.data.frame(matrix(nrow = nrow(dataset), 
                                               ncol = 1))
        
        # create c a column index for the converted data
        c = 1
        converted_data[,c] <- as.factor(dataset[,i])
        
        # add in the column names to the dataset
        colnames(converted_data)[c] <- colnames(dataset)[i]
        
        # update column index c
        c = c + 1
        
      }
      
    }
    
  } else if(convert.to == "numeric" & convert.to == "double"){
    
    for (i in 1:ncol(dataset)){
      
      if(is.numeric(dataset[,i])){
        
        # create empty dataframe to store the interaction terms 
        converted_data <- as.data.frame(matrix(nrow = nrow(dataset), 
                                               ncol = 1))
        
        # create c a column index for the converted data
        c = 1
        converted_data[,c] <- as.double(dataset[,i])
        
        # add in the column names to the dataset
        colnames(converted_data)[c] <- colnames(dataset)[i]
        
        # update column index c
        c = c + 1
        
      }
      
    }
    
  } else if(convert.to == "numeric" & convert.to == "integer"){
    
    for (i in 1:ncol(dataset)){
      
      if(is.numeric(dataset[,i])){
        
        # create empty dataframe to store the interaction terms 
        converted_data <- as.data.frame(matrix(nrow = nrow(dataset), 
                                               ncol = 1))
        
        # create c a column index for the converted data
        c = 1
        converted_data[,c] <- as.integer(dataset[,i])
        
        # add in the column names to the dataset
        colnames(converted_data)[c] <- colnames(dataset)[i]
        
        # update column index c
        c = c + 1
        
      }
      
    }
    
  } else if(convert.from == "numeric" & convert.to == "character"){
    
    for (i in 1:ncol(dataset)){
      
      if(is.numeric(dataset[,i])){
        
        # create empty dataframe to store the interaction terms 
        converted_data <- as.data.frame(matrix(nrow = nrow(dataset), 
                                               ncol = 1))
        
        # create c a column index for the converted data
        c = 1
        converted_data[,c] <- as.character(dataset[,i])
        
        # add in the column names to the dataset
        colnames(converted_data)[c] <- colnames(dataset)[i]
        
        # update column index c
        c = c + 1
        
      }
      
    }
    
  } else if(convert.from == "numeric" & convert.to == "factor"){
    
    for (i in 1:ncol(dataset)){
      
      if(is.numeric(dataset[,i])){
        
        # create empty dataframe to store the interaction terms 
        converted_data <- as.data.frame(matrix(nrow = nrow(dataset), 
                                               ncol = 1))
        
        # create c a column index for the converted data
        c = 1
        converted_data[,c] <- as.factor(dataset[,i])
        
        # add in the column names to the dataset
        colnames(converted_data)[c] <- colnames(dataset)[i]
        
        # update column index c
        c = c + 1
        
      }
      
    }
    
  } else if(convert.from == "integer" & convert.to == "double"){
    
    for(i in 1:ncol(dataset)){
      
      if(is.integer(dataet[,i])){
        
        # create empty dataframe to store the interaction terms 
        converted_data <- as.data.frame(matrix(nrow = nrow(dataset), 
                                               ncol = 1))
        
        # create c a column index for the converted data
        c = 1
        converted_data[,c] <- as.double(dataset[,i])
        
        # add in the column names to the dataset
        colnames(converted_data)[c] <- colnames(dataset)[i]
        
        # update column index c
        c = c + 1
        
      }
      
    }
    
  } else if(convert.from == "integer" & convert.to == "numeric"){
    
    for(i in 1:ncol(dataset)){
      
      if(is.integer(dataet[,i])){
        
        # create empty dataframe to store the interaction terms 
        converted_data <- as.data.frame(matrix(nrow = nrow(dataset), 
                                               ncol = 1))
        
        # create c a column index for the converted data
        c = 1
        converted_data[,c] <- as.numeric(dataset[,i])
        
        # add in the column names to the dataset
        colnames(converted_data)[c] <- colnames(dataset)[i]
        
        # update column index c
        c = c + 1
        
      }
      
    }
    
  } else if(convert.from == "integer" & convert.to == "character"){
    
    for(i in 1:ncol(dataset)){
      
      if(is.integer(dataet[,i])){
        
        # create empty dataframe to store the interaction terms 
        converted_data <- as.data.frame(matrix(nrow = nrow(dataset), 
                                               ncol = 1))
        
        # create c a column index for the converted data
        c = 1
        converted_data[,c] <- as.character(dataset[,i])
        
        # add in the column names to the dataset
        colnames(converted_data)[c] <- colnames(dataset)[i]
        
        # update column index c
        c = c + 1
        
      }
      
    }
    
  } else if(convert.from == "integer" & convert.to == "factor"){
    
    for(i in 1:ncol(dataset)){
      
      if(is.integer(dataet[,i])){
        
        # create empty dataframe to store the interaction terms 
        converted_data <- as.data.frame(matrix(nrow = nrow(dataset), 
                                               ncol = 1))
        
        # create c a column index for the converted data
        c = 1
        converted_data[,c] <- as.factor(dataset[,i])
        
        # add in the column names to the dataset
        colnames(converted_data)[c] <- colnames(dataset)[i]
        
        # update column index c
        c = c + 1
        
      }
      
    }
    
  } else if(convert.from == "character" & convert.to == "double"){
    
    for(i in 1:ncol(dataset)){
      
      if(is.character(dataset[,i])){
        
        # create empty dataframe to store the interaction terms 
        converted_data <- as.data.frame(matrix(nrow = nrow(dataset), 
                                               ncol = 1))
        
        # create c a column index for the converted data
        c = 1
        converted_data[,c] <- as.double(dataset[,i])
        
        # add in the column names to the dataset
        colnames(converted_data)[c] <- colnames(dataset)[i]
        
        # update column index c
        c = c + 1
        
      }
      
    }
    
  } else if(convert.from == "character" & convert.to == "numeric"){
    
    for(i in 1:ncol(dataset)){
      
      if(is.character(dataset[,i])){
        
        # create empty dataframe to store the interaction terms 
        converted_data <- as.data.frame(matrix(nrow = nrow(dataset), 
                                               ncol = 1))
        
        # create c a column index for the converted data
        c = 1
        converted_data[,c] <- as.numeric(dataset[,i])
        
        # add in the column names to the dataset
        colnames(converted_data)[c] <- colnames(dataset)[i]
        
        # update column index c
        c = c + 1
        
      }
      
    }
    
  } else if(convert.from == "character" & convert.to == "integer"){
    
    for(i in 1:ncol(dataset)){
      
      if(is.character(dataset[,i])){
        
        # create empty dataframe to store the interaction terms 
        converted_data <- as.data.frame(matrix(nrow = nrow(dataset), 
                                               ncol = 1))
        
        # create c a column index for the converted data
        c = 1
        converted_data[,c] <- as.integer(dataset[,i])
        
        # add in the column names to the dataset
        colnames(converted_data)[c] <- colnames(dataset)[i]
        
        # update column index c
        c = c + 1
        
      }
      
    }
    
  } else if(convert.from == "character" & convert.to == "factor"){
    
    for(i in 1:ncol(dataset)){
      
      if(is.character(dataset[,i])){
        
        # create empty dataframe to store the interaction terms 
        converted_data <- as.data.frame(matrix(nrow = nrow(dataset), 
                                               ncol = 1))
        
        # create c a column index for the converted data
        c = 1
        converted_data[,c] <- as.factor(dataset[,i])
        
        # add in the column names to the dataset
        colnames(converted_data)[c] <- colnames(dataset)[i]
        
        # update column index c
        c = c + 1
        
      }
      
    }
    
  } else if(convert.from == "factor" & convert.to == "double"){
    
    for (i in 1: ncol(dataset)){
      
      if(is.factor(dataset[,i])){
        
        # create empty dataframe to store the interaction terms 
        converted_data <- as.data.frame(matrix(nrow = nrow(dataset), 
                                               ncol = 1))
        
        # create c a column index for the converted data
        c = 1
        converted_data[,c] <- as.double(dataset[,i])
        
        # add in the column names to the dataset
        colnames(converted_data)[c] <- colnames(dataset)[i]
        
        # update column index c
        c = c + 1
        
      }
      
    }
    
  } else if(convert.from == "factor" & convert.to == "numeric"){
    
    for (i in 1: ncol(dataset)){
      
      if(is.factor(dataset[,i])){
        
        # create empty dataframe to store the interaction terms 
        converted_data <- as.data.frame(matrix(nrow = nrow(dataset), 
                                               ncol = 1))
        
        # create c a column index for the converted data
        c = 1
        converted_data[,c] <- as.numeric(dataset[,i])
        
        # add in the column names to the dataset
        colnames(converted_data)[c] <- colnames(dataset)[i]
        
        # update column index c
        c = c + 1
        
      }
      
    }
    
  } else if(convert.from == "factor" & convert.to == "integer"){
    
    for (i in 1: ncol(dataset)){
      
      if(is.factor(dataset[,i])){
        
        # create empty dataframe to store the interaction terms 
        converted_data <- as.data.frame(matrix(nrow = nrow(dataset), 
                                               ncol = 1))
        
        # create c a column index for the converted data
        c = 1
        converted_data[,c] <- as.integer(dataset[,i])
        
        # add in the column names to the dataset
        colnames(converted_data)[c] <- colnames(dataset)[i]
        
        # update column index c
        c = c + 1
        
      }
      
    }
    
  } else if(convert.from == "factor" & convert.to == "character"){
    
    for (i in 1: ncol(dataset)){
      
      if(is.factor(dataset[,i])){
        
        # create empty dataframe to store the interaction terms 
        converted_data <- as.data.frame(matrix(nrow = nrow(dataset), 
                                               ncol = 1))
        
        # create c a column index for the converted data
        c = 1
        converted_data[,i] <- as.character(dataset[,i])
        
        # add in the column names to the dataset
        colnames(converted_data)[c] <- colnames(dataset)[i]
        
        # update column index c
        c = c + 1
        
      }
      
    }
    
  }
  
  if(!is.null(directory)) {
    
    write.csv(x = converted_data, 
              file = paste(directory, "/", file_name, sep = ""), 
              row.names = F)
    
  }
  
  # return the converted data
  return(converted_data)
  
}
oislen/BuenaVista documentation built on May 16, 2019, 8:12 p.m.