If you are using rbind
/cbind
to build some results from an iterative procedure, you may declare the "empty" object to store the data in. For numeric data, use numeric(0)
, which is a zero-length numeric vector. It is compatible with any binding:
rbind(numeric(0), 1:3)
[,1] [,2] [,3]
[1,] 1 2 3
cbind(numeric(0), 1:3)
[,1]
[1,] 1
[2,] 2
[3,] 3
The same holds for NULL
(as pointed by @jbaums). It may even be more convenient since you don't have to specify the data type manually (the same will also work with numeric(0)
due to implicit type conversion):
rbind(NULL, letters[1:3])
[,1] [,2] [,3]
[1,] "a" "b" "c"