#' 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'
#' @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])
}
}
df
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.