#' Convert to numeric
#'
#' @param x dataframe or character
#'
#' @return numeric
#' @export
#'
to_numeric <- function(x){
if (is.data.frame(x)){
for (i in 1:ncol(x)) {
xi <- tryCatch(as.numeric(x[,i]),error=function(e) 'error',warning=function(w) 'error')
if (all(xi=='error')) next(i)
x[,i] <- xi
}
}else if (is.character(x)){
x <- as.numeric(x)
}
return(x)
}
#' Convert to numeric
#'
#' @param x dataframe or character
#'
#' @return numeric
#' @export
#'
`to_numeric<-` <- function(x,value){
if (is.data.frame(x)){
for (i in 1:length(value)) {
xi <- tryCatch(as.numeric(x[,value[i]]),error=function(e) 'error',warning=function(w) 'error')
if (all(xi=='error')) next(i)
x[,value[i]] <- xi
}
}else if (is.character(x)){
x <- as.numeric(x)
}
return(x)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.