我是 R 的新手,我想知道是否有人可以解释is.vector()
和is.numeric()
之间的区别。
is.numeric
is a general test to check whether a vector is numeric or not. It will return TRUE
only if the object passed to it is a vector and consists of only numeric data. Whereas, is.vector
tests whether the object is a vector or not. It will return TRUE
if the object passed is a vector.
is.numeric
returns true if the base type of the class is double
or integer
and when values can reasonably be regarded as numeric. (they should be eligible for arithmetic operations and comparable by their base class)
For any values enclosed in a vector being numeric, (integer and floating-point/decimal values) is.numeric
triggers as TRUE
.
Whereas is.vector
checks the class of your object, whether it falls under the conditions of being a vector. Vectors include different variants pertaining to the type of its contents, which can be numeric
(strictly numbers inclusive of decimal values) or character
or logical (having boolean values TRUE
or FALSE
) or the vector can be raw or complex as well. (You can check the type of vector using typeof()
function)
The distinction is clear as for character values or boolean values in your vector, is.numeric
would return FALSE
as opposed to a TRUE
for is.vector
.
If you were to test the same for data frames instead of vectors, the question would arise between isNumeric
and is.data.frame
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.