I wonder, how can I create a numeric zero-length vector in R?
R : Create a vector of zeros using the numeric() function In R, the numeric() function creates objects of numeric type. The numeric() function will create a double-precision vector of the specified length in the argument with all elements value equal to zero.
In terms of components, the zero vector in two dimensions is 0=(0,0), and the zero vector in three dimensions is 0=(0,0,0). If we are feeling adventurous, we don't even need to stop with three dimensions. If we have an arbitrary number of dimensions, the zero vector is the vector where each component is zero.
To create a vector of specified data type and length in R we make use of function vector(). vector() function is also used to create empty vector.
If you read the help for vector
(or numeric
or logical
or character
or integer
or double
, 'raw' or complex
etc ) then you will see that they all have a length
(or length.out
argument which defaults to 0
Therefore
numeric() logical() character() integer() double() raw() complex() vector('numeric') vector('character') vector('integer') vector('double') vector('raw') vector('complex')
All return 0 length vectors of the appropriate atomic modes.
# the following will also return objects with length 0 list() expression() vector('list') vector('expression')
Simply:
x <- vector(mode="numeric", length=0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With