tags:

views:

111

answers:

7

Hi everyone!

How to create an "empty object" in R? [edit: I don't know how to rightly call this "thing" so I am calling it "empty object", others: "empty symbol", "zero length symbol", "missing object" might also be used]

[edit2: finally I tend to settle down on the "missing symbol object" for the name of the "thing". It also appears that J.Chambers is using this terminology in his 2008 book, see comments for @mbq's answer. According to Chambers, the "missing symbol" has a zero-length string as it's contents. Thus, as.symbol("") should create such an object, which it doesn't in current version of R(2.11.1) ]

The simplest way I could find is

x <- alist(a=)$a

[Clarification]

Note that "empty object" is not a NULL object or a vector of length 0. "Empty object" x in my above example could be used in the function's formals manipulation, which is what I need it for.

Here is an example:

> al <- alist(a = 323, b = , c = 434)  
> al  
$a  
[1] 323  
$b  
$c  
[1] 434  

>   
> al[["c"]] <- numeric()  
> al  
$a  
[1] 323  
$b  
$c                                   #not empty  
numeric(0)  

>   
> al[["c"]] <- list()              
> al  
$a  
[1] 323  
$b  
$c                                   #not empty  
list()  

>   
>   
> al[["c"]] <- NULL                    #object removed  
> al  
$a  
[1] 323  
$b  


>   
> al[["c"]] <- alist(a = )$a  
> al  
$a  
[1] 323  
$b  
$c                                      #empty  

So, I am just looking for a way to create empty objects for use in function's formals manipulations. I am pretty sure there must be a way in base R.

Here is an example:

> foo <- function(a = 3232, b = 234){b+a}  
> formals(foo)  
$a  
[1] 3232  

$b  
[1] 234  

> formals(foo)$c <- alist(a = )$a  
> formals(foo)  
$a  
[1] 3232  

$b  
[1] 234  

$c  


> foo <- function(a = 3232, b = 234){b+a}  
> formals(foo)  
$a  
[1] 3232  

$b  
[1] 234  

> formals(foo)$c <- alist(a = )$a  
> formals(foo)  
$a  
[1] 3232  

$b  
[1] 234  

$c  

Thanks.

+2  A: 

Depending on the type :

x <- list()
# empty vectors
y <- numeric(0)
z <- logical(0)
...

Now you have to think whether you really want to create an empty object.

edit : You can indeed use the NULL option as well. The main difference is that the NULL object doesn't have a type. See also ?"NULL"

Joris Meys
`numeric(0)` is equal to `numeric()`, same with `logical()`.
mbq
I need empty object not just an object of length 0. I will edit the Q to be more precise.
VitoshKa
@VitoshKa -- there are no empty objects in R, only `NULL` object to plus-minus serve this purpose. See my answer.
mbq
@mbq -- and how would you call the object I have created in my example then?
VitoshKa
A: 

Also d <- c()

Stedy
Whoever down voted-this, at least try to explain why you're down-voting.
Brandon Bertelsen
+4  A: 

What have you done with alist(a=)$a is not an empty object, it is empty symbol/name ('' compiled as symbol name). I'm pretty sure it can't be replicated in any other way (as.symbol('') raises error).

mbq
x holds a value, which is an object. I would not be so sure a symbol is a better name for that "thing" :)
VitoshKa
well, after a bit of more thinking, it's indeed an unevaluated expression, which is "empty". So probably technically it would be rightly to ask for empty symbol sort of. On the other hand a name in R is also an object so it's not wrong either.
VitoshKa
See `is.symbol(formals(plot)$x)` and `deparse(formals(plot)$x)`. This suggests that `as.symbol("")` is the right way to go about it, but it doesn't work in the current version of R.
hadley
Yes, indeed that should be the case. Here is the excerpt from (Chambers 2008, p55): "One odd detail relates to arguments that donot have a default expression. The element in the formals list corresponding to x appears to be empty, but it must be an object.(Every thing is an object.) In fact,it is an anomalous name or symbol object in R, with an empty string as its content." So it's "missing symbol object" what would be the proper name for the "thing".
VitoshKa
@VitoshKa it "symbol" is just the way it is called in R; the problem was just that you've been looking for some other thing: pairlist with empty binding of c. @hadley But you must admit this is what this thing really is.
mbq
A: 

Also d <- ""

(padding padding padding)

Brandon Bertelsen
A: 

There is no 'empty object'. There's NULL, NA (in various flavours), zero-length lists and so on. Where does your concept of 'empty object' come from, and what are the properties of an 'empty object'?

What you have created with alist is a monster:

> x<-alist(a=)$a
> x
Error: argument "x" is missing, with no default

[its some kind of unevaluated function argument expression thing. Not an 'empty object' in any sense of the word I know]

Spacedman
Yes indeed its an unevaluated empty "thing".
VitoshKa
+2  A: 

As others have said, you're not creating an empty object. x holds a value, which is an unevaluated expression.

?alist says:

‘alist’ handles its arguments as if they described function arguments. So the values are not evaluated, and tagged arguments with no value are allowed whereas ‘list’ simply ignores them. ‘alist’ is most often used in conjunction with ‘formals’.

This is the easiest way I can determine to add an argument with no value to a function:

> foo <- function(a = 3232, b = 234){b+a}
> formals(foo) <- c(formals(foo),alist(c=))
> formals(foo)
$a
[1] 3232

$b
[1] 234

$c
Joshua Ulrich
Unevaluated expresionas are also objects:) . And how about replacement of c in formals? Your solution will try to create two c's inside if c is already present. The funny thing is that R will allow it!
VitoshKa
I completely agree, which is why asking how to create an "empty" object makes no sense.
Joshua Ulrich
My solution won't create two "c's", it adds another expression to the pairlist. You're trying to program on the language, which is very different than programming with the language. You need to read [this section](http://cran.r-project.org/doc/manuals/R-lang.html#Computing-on-the-language) of the *R Language Definition*. R allows it because it's a valid expression.
Joshua Ulrich
I've said "if c is already present", formals(foo)<-c(formals(foo),alist(b=)) and "b" will be doubled. I think "missing symbol" would finally be the right word for the thing.
VitoshKa
+4  A: 

Try substitute():

foo <- function(a = 3232, b = 234) {}
formals(foo)$c <- substitute()
foo
# function (a = 3232, b = 234, c)
hadley
Yes, that is it!!! It creates this elusive "thing".
VitoshKa
`bquote` currently does the same thing. I personally wouldn't rely on this behavior, since it is undocumented and there are other ways to achieve the same result.
Joshua Ulrich
@Joshua Ulrich -- Yap! Good point.
VitoshKa
Best way to find out is to ask R-core, which I've just done.
hadley
+1 You've beat me, I've missed the question update.
mbq