#' Change the data type of the columns in data frame (version 2)
#'
#' 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 list of vectors of integers specifyung which columns to coerce. Must be non-redundant.
#' @param type One of 'numeric', 'character', 'factor', 'integer'
#' @export
changeTypeOfColumns.2 <- function(df, rangeOfColumns, type){
if(class(rangeOfColumns) != "integer") stop("rangeOfColumns paramter is not integer. Exiting.")
if(!(type %in% c("numeric","character","factor", "integer"))) stop("data type specification is not one of 'numeric', 'character', or 'factor'")
for(j in 1:length(rangeOfColumns)){
for( i in rangeOfColumns[[j]]) {
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])
}
if(type == "integer"){
df[,i] = as.integer(df[,i])
}
}
}
df
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.