#' Check if a string vector contains numbers only.
#'
#' @param arg a vector or a list
#' @param pat = 1 to match non-negative integers \cr
#'  = 2 to match signed real numbers \cr
#'  = 3 to match integers
#'
#' @details
#' The leading and trailing blank characters will be ignored.

#' @examples
#' vec=c("a","1","1.4","b.","-b","-2","2-"," 12 ","5 5","0.4",".4","-0.","-0.8")
#' is.number(vec)
#' printm(vec,is.number(as.list(vec)),is.number(as.list(vec), 2))
#' is.number( list( c("1","2","a"), c(1,2) ) )
#' is.number( list( 2, -2,"-2","-2." ), 3 )
#'
#' @return
#' If arg is a vector or matrix, a scalar TRUE or FALSE. \cr
#' If arg is a list, a vector of the same length as the arg consisting of
#' TRUEs or FALSEs.
#'
#' @export
#'

is.number <- function( arg, pat=1 ){
 # check if all the elements are numbers.
 # Shin-ichi Mayekawa
 # 20160731,0801
 # new method and pat=3: 20220927dnc
 #

 # pattern to match
 pattern=c( "^ *[[:digit:]]+ *$",
 "(^ *-?[[:digit:]]+\\.?[[:digit:]]* *$)|(^ *-?[[:digit:]]*\\.?[[:digit:]]+ *$)"
 ,"^ *-?[[:digit:]]+ *$" )


 if( is.list(arg) ){
  res=unlist( lapply( arg, function(x){ regexpr(pattern[pat], x) > 0 } ) )
 }
 else{
  if( is.numeric( arg ) ) res=TRUE
  else{
   resg=grep(pattern[pat], arg)
   if( length(arg) == length(resg) ) res=TRUE
   else res=FALSE
  }
 }

 return( res )

} # is.number


