#' Convert the columns of a data frame to a specific type/class.
#'
#' @param X A Data Frame or a Matrix
#' @param what A character vector of class names, or "ANY" to match any class
#' \cr where class names can be "factor", "character", "numeric" etc.
#' @param func The function to be used for the conversion.
#'
#' @details
#' This function returns \cr
#' \code{ rapply( X, func, classes=what, how="replace" ) }
#' \cr\cr
#' The same can be obtained by:\cr
#' \code{ loc <- which( sapply(vartype(X), "\%in\%", what ) ) }\cr
#' \code{ X[loc] <- lapply( X[loc], func ) }\cr
#'
#' @examples
#' A <- demomat(5,3,root="")
#' Af <- as.data.frame(A)
#' Af[,2]=as.numeric(Af[,2])
#' str(Af)
#'
#' Ac <- convert( Af )
#' str(Ac)
#'
#' Ac <- convert( Af, what=c("factor","numeric") )
#' str(Ac)
#'
#' @return X with the converted values.
#'
#' @export
#'

convert <- function( X, what="factor", func=as.character ){
 # convert all factors to character
 # Shin-ichi Mayekawa
 # 20170917
 #

 if( length(dim(X)) != 2 ){
  cat("\n\nerror1:(convert) Input must be a matrix or data frame.\n\n")
  stop()
 }
 else if( is.matrix(X) ) X=as.data.frame(X)
 X=rapply( X, func, classes=what, how="replace" )
 return( X )
} # end of convert


