R/col2factor.R

Defines functions col2factor

Documented in col2factor

#'Transform defined columns to factor.
#'
#' @description
#' col2factor transform columns type to factor.
#'
#' @param x a dataframe
#' @param start number of start column
#' @param end number of last column (default=last)
#'
#' @return Return a dataframe with transformed columns.
#' @export
#'
#' @examples
#' v=data.frame(c(3,2,5,6,5,4))
#' class(v[,1]) #here class is numeric
#' v=col2factor(v,1)
#' class(v[,1]) #now class is character
#'
col2factor=function(x,start,end=ncol(x)){
    a=x
    a[,start:end]=sapply(a[,start:end],FUN = as.factor)
    return(a)
}

Try the metools package in your browser

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

metools documentation built on July 2, 2020, 2:28 a.m.