R/recodeColumns.r

Defines functions recodeColumns

Documented in recodeColumns

#' Recodes factors with more than \code{maxLevels} to characters.
#' @param df the data frame to recode.
#' @param maxLevels the maximum number of levels a factor can have before being
#'        converted to a character.
recodeColumns <- function(df, maxLevels=20) {
	for(c in seq_len(ncol(df))) {
		if(class(df[,c])[1] == 'factor' & length(levels(df[,c])) > maxLevels) {
			df[,c] = as.character(df[,c])
		}
	}
	return(df)	
}

Try the sqlutils package in your browser

Any scripts or data that you put into this service are public.

sqlutils documentation built on May 29, 2017, 10:42 a.m.