Check if All Elements in Character Vector are Numeric
all.is.numeric.RdTests, without issuing warnings, whether all elements of a character
vector are legal numeric values, or optionally converts the vector to a
numeric vector. Leading and trailing blanks in x are ignored.
Arguments
- x
a character vector
- what
specify
what="vector"to return a numeric vector if it passes the test, or the original character vector otherwise, the default"test"to returnFALSEif there are no non-missing non-extravalues ofxor there is at least one non-numeric value ofx, or"nonnum"to return the vector of non-extra, non-NA, non-numeric values ofx.- extras
a vector of character strings to count as numeric values, other than
"".
Examples
all.is.numeric(c('1','1.2','3'))
#> [1] TRUE
all.is.numeric(c('1','1.2','3a'))
#> [1] FALSE
all.is.numeric(c('1','1.2','3'),'vector')
#> [1] 1.0 1.2 3.0
all.is.numeric(c('1','1.2','3a'),'vector')
#> [1] "1" "1.2" "3a"
all.is.numeric(c('1','',' .'),'vector')
#> [1] 1 NA NA
all.is.numeric(c('1', '1.2', '3a'), 'nonnum')
#> [1] "3a"