R/factor.it.numeric.it.R

Defines functions `numeric.it<-` numeric.it factor.it `factor.it<-`

Documented in factor.it numeric.it

#' Change data type
#'
#' @param x dataframe
#' @param value column names
#'
#' @return factor or numeric columns in a dataframe
#' @export
#' @name columntrans
#' @examples
#' str(mtcars)
#' factor.it(mtcars,c("cyl", "vs", "am", "gear"))
#' factor.it(mtcars)=c("cyl", "vs", "am", "gear")
#' str(mtcars)
#' 
#' numeric.it(mtcars,c("cyl", "vs", "am", "gear"))
#' numeric.it(mtcars)=c("cyl", "vs", "am", "gear")
#' str(mtcars)
`factor.it<-` <- function(x,value){
    if (!is.data.frame(x)) stop('x must be a dataframe')
    if (any(! value %in% colnames(x))) stop('value must be column names in x')
    for (i in value) {
        x[,i]=factor(x[,i])
    }
    x
}
#' @export
#' @rdname columntrans
factor.it <- function(x,value){
    if (!is.data.frame(x)) stop('x must be a dataframe')
    if (any(! value %in% colnames(x))) stop('value must be column names in x')
    for (i in value) {
        x[,i]=factor(x[,i])
    }
    x
}
#' @export
#' @rdname columntrans
numeric.it <- function(x,value){
    if (!is.data.frame(x)) stop('x must be a dataframe')
    if (any(! value %in% colnames(x))) stop('value must be column names in x')
    for (i in value) {
        x[,i]=as.numeric(as.character(x[,i]))
    }
    x
}
#' @export
#' @rdname columntrans
`numeric.it<-` <- function(x,value){
    if (!is.data.frame(x)) stop('x must be a dataframe')
    if (any(! value %in% colnames(x))) stop('value must be column names in x')
    for (i in value) {
        x[,i]=as.numeric(as.character(x[,i]))
    }
    x
}

Try the do package in your browser

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

do documentation built on Aug. 3, 2021, 5:06 p.m.