R/str_is_numeric.R

Defines functions str_is_numeric

Documented in str_is_numeric

#' Find and return values that look non-numeric
#'
#' @param xx input vector
#'
#' @return logical vector the same length as `xx` indicating whether values convert to numeric or not
#' @export
#'
#' @examples
#' str_is_numeric( as.factor( c( 'a', 1:5, NA) ) )
str_is_numeric <- function(xx){

	if( is.numeric(xx) ){
		return( TRUE )
	} else {
		if( is.factor(xx) ) xx = as.character(xx)
		prena = is.na( xx )
		postna = is.na( suppressWarnings(as.numeric(xx)) )
		return( !(postna & !prena) )
	}
}
stackcon/rngt documentation built on June 17, 2022, 5:29 p.m.