# [R] how to insert NULLs in lists?

markleeds at verizon.net markleeds at verizon.net
Sat May 23 01:32:47 CEST 2009

```   Hi Kynn: this oddityÂ  is discussed in Patrick Burn's document called "The R
Inferno". I don't recall the fix so I'm not sure if below is the same as
what his book says to do but it seems to do what you want.
x <- list()
x[[1]] <- 2
x
length(x)
print(str(x))
x[2] <- list(NULL)
x
length(x)
print(str(x))
Still,  I would look at that document rather than just using the above
because I'm not an expeRt
so there might be a better way ?
Also, if you cant find Pat's website, let me know andÂ  I'll find it. I'm
pretty sure that,Â  if you
google  "Patrick Burns", his site should be up at the top and then his
R-Inferno is easy to
find from there. It's quite a useful document so I highly recommend it.

On May 22, 2009, Kynn Jones <kynnjo at gmail.com> wrote:

I'm an experienced programmer, but learning R is making me lose the little
hair I have left...
> list(NULL)
[[1]]
NULL
> length(list(NULL))
[1] 1
> x <- list()
> x[[1]] <- NULL
> x
list()
> length(x)
[1] 0
>From the above experiment, it is clear that, although one can create a
one-element list consisting of a NULL element, one can't get the same
result
by assigning NULL to the first element of an empty list. And it gets
worse:
> x <- list(1, 2, 3)
> length(x)
[1] 3
> x[[2]] <- NULL
> length(x)
[1] 2
I just could NOT believe my eyes! Am I going crazy???
What I'm trying to do is so simple and straightforward: I want to be able
to
append NULL to a list, and, after the appending, have the last element of
the list be NULL. Is that so unreasonable? How can it be done?
TIA!
Kynn
[[alternative HTML version deleted]]
______________________________________________
[1]R-help at r-project.org mailing list
[2]https://stat.ethz.ch/mailman/listinfo/r-help