Is it possible to create objects in R with length 0?
-
Is it possible? Yes. Should you do it? Usually no. – Roland Sep 23 '16 at 07:37
-
2@NikolasRieble Please don't recommend Stack Overflow for this. It's not a good question and can be answered by a quick search on the internet. It wouldn't be well received at SO. – Roland Sep 23 '16 at 07:39
-
@Roland Rather than asking the question there, most likely you will find an answer there. Which is why I recommend SO as the place to search first. Further, to my understanding of crossvalidated and SO - pure programming problems do not have a place here but at SO. – Nikolas Rieble Sep 23 '16 at 07:42
-
3@NikolasRieble Recommending SO as a place to search is fine, but that's not what you did. There is a kind of contract between the sites. "We try to not send you subpar questions and you do us the same courtesy." – Roland Sep 23 '16 at 07:48
-
@Roland You are right, thanks for clarification. – Nikolas Rieble Sep 23 '16 at 07:52
-
4`x <- NULL` is the popular way to initialize placeholder for any object. – Tim Sep 23 '16 at 08:04
2 Answers
Sure:
a<-vector()
length(a)

- 51,121
- 6
- 32
- 66

- 81
- 5
-
This does not provide an answer to the question. Once you have sufficient [reputation](http://stats.stackexchange.com/help/whats-reputation) you will be able to [comment on any post](http://stats.stackexchange.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/117635) – Xi'an ні війні Sep 23 '16 at 07:31
-
1The question is "Is it possible to...?" and the answer is "Sure" plus an example of how to do it. How does this not provide an answer to the question? (I am asking since I am new at CV and I am getting to know the culture of CV such as way answers are recieved). How could Ema Nymton give a better answer? – Nikolas Rieble Sep 23 '16 at 07:47
-
1@Xi'an I agree with Nikolas. I don't see how this answer can be described as not providing an answer to the question that was asked. – mark999 Sep 23 '16 at 08:34
-
Popular way to initialize placeholder for any object in R is
x <- NULL
length(x)
## 0
class(x)
## "NULL"
str(x)
## NULL
Typical usage would be:
x <- NULL
for (i in 1:10)
x[i] <- i
x <- NULL
for (i in 1:10)
x[[i]] <- letters[1:10]
As you can see, R on-the-fly changes x
to appropriate (from it's point of view..) class. In case as above the object is populated and expanded, so it has length zero only until you fill it with something. You can always declare object of predefined size, x <- numeric(10)
(often preferable from performance point of view), and then if you populated it with less then 10 values, the rest with be filled with default values (zeros in case of numeric
), and if you fill it with more then 10 values it will be appropriately expanded.
As already noted, you can also initialize objects with predefined class, e.g.
x <- list()
x <- vector()
x <- numeric()
x <- data.frame()
etc.
However beware because - as always - there are exceptions, e.g.
x <- matrix()
dim(x)
## [1] 1 1
x
## [,1]
## [1,] NA

- 7,075
- 6
- 29
- 58
-
Please, please, please, at least warn against growing objects in a loop and show how to pre-allocate to the final object size. – Roland Sep 23 '16 at 10:22
-
@Roland but isn't the first obvious and second not related to the question..? – Tim Sep 23 '16 at 10:25
-
Obvious to you maybe, but certainly not to OP. And you show how to grow an object in a loop. – Roland Sep 23 '16 at 10:25
-
@Roland ok, done. Is it something that you had in mind, or want to add something? – Tim Sep 23 '16 at 10:29
-
1The main point is that growing an object is one of the slowest operations you can do using a programming language. If you know that you want to iterate over `1:10` you know that your final size is 10 and usually also know the class of the object. Thus, you can pre-allocate easily and potentially save hours to days of computing time (I have seen real-life examples of this). – Roland Sep 23 '16 at 10:32
-
@Roland totally agree and I mentioned it in my edit. But in most non-huge applications this is not really an issue and `NULL` placeholders are the most convenient way to initialize object... – Tim Sep 23 '16 at 10:37
-
1Just keep in mind that many beginners don't know the potential performance issues. Don't just hand them the gun. They will shoot themselves in the foot. – Roland Sep 23 '16 at 10:40