Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a numeric vector of zero length in R

I wonder, how can I create a numeric zero-length vector in R?

like image 828
Surjya Narayana Padhi Avatar asked Sep 27 '12 05:09

Surjya Narayana Padhi


People also ask

How do I make a vector of zeros 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.

How do you make a vector 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.

How do you create a vector of length n in R?

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.


2 Answers

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') 
like image 153
mnel Avatar answered Oct 03 '22 11:10

mnel


Simply:

x <- vector(mode="numeric", length=0) 
like image 35
srctaha Avatar answered Oct 03 '22 09:10

srctaha