It seems that some programmers are using:
a = {}
a$foo = 1
a$bar = 2
What is the benefit over a = list(foo = 1, bar = 2)
?
Why should {}
be used? This expression only returns NULL
, so a NULL
assignment would do the same, wouldn't it?
It seems that some programmers are using:
a = {}
a$foo = 1
a$bar = 2
What is the benefit over a = list(foo = 1, bar = 2)
?
Why should {}
be used? This expression only returns NULL
, so a NULL
assignment would do the same, wouldn't it?
Your first query
Yes,
a <- NULL
gives the same effect. Using{}
is likely to be a personal style.NULL
NULL
is probably the most versatile and confusing R object. From R language definition of NULL:Strictly speaking,
NULL
is justNULL
. And it is the only thing thatis.null
returnsTRUE
. However, according to?NULL
:So, while it is not identical to a length-0 vector with a legitimate mode (not all modes in R are allowed in a vector; read
?mode
for the full list of modes and?vector
for what are legitimate for a vector), this flexible coercion often makes it behave like a length-0 vector:You can do vector concatenation:
You can grow a vector (as you did in your question):
Using
{}
to generateNULL
is similar toexpression()
. Though not identical, the run-time coercion when you later do something with it really makes them indistinguishable. For example, when growing a list, any of the following would work:For a length-0 vector with an atomic mode, a warning is produced during run-time coercion (because the change from "atomic" to "recursive" is too significant):
We don't get a warning for expression setup, because from
?expression
:Well, it is not a "list" in the usual sense; it is an abstract syntax tree that is list-alike.
Your second query
There is no advantage in doing so. You should have already read elsewhere that growing objects is a bad practice in R. A random search on Google gives: growing objects and loop memory pre-allocation.
If you know the length of the vector as well as the value of its each element, create it directly, like
a = list(foo = 1, bar = 2)
.If you know the length of the vector but its elements' values are to be computed (say by a loop), set up a vector and do fill-in, like
a <- vector("list", 2); a[[1]] <- 1; a[[2]] <- 2; names(a) <- c("foo", "bar")
.In reply to Tjebo
It means the source of an R distribution, which is a ".tar.gz" file on CRAN. An alternative is look up on https://github.com/wch/r-source. Either way, this is the table:
Per R's documentation on braces and parentheses (type
?'{'
to read them), braces return the last expression evaluated within them.In this case,
a <- {}
essentially "returns" a null object, and is therefore equivalent toa <- NULL
, which establishes an empty variable that can then be treated as a list.Incidentally, this is why it is possible to write R functions in which the function's output is returned simply by writing the name of the returned variable as the function's final statement. For example:
Is equivalent to:
Or even:
The final line of the function being an assignment suppresses printing of the result in the console, but the function definitely returns the expected value if it saved into a variable.