[R] how to insert NULLs in lists?

Bert Gunter gunter.berton at gene.com
Sat May 23 01:24:43 CEST 2009


Continuing your example below,

x[4] <- list(NULL)

I won't try to defend the semantics,which have been complained about before.
However, note that with lists, x[i] is the list which consists of one
member, the ith component of x, which is not the same as x[[i]], the ith
component, itself; so the assignment above sets the list that contains the
4th component of x to the list that contains NULL, doing what you desire.
 For similar reasons, x <- c(x,list(NULL)) would also work.

I believe all of this is documented in the man page for "[" ; but I grant
that it takes some close reading and experimentation to get it.

-- Bert Gunter
Genentech Nonclinical Statistics

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Kynn Jones
Sent: Friday, May 22, 2009 3:52 PM
To: r-help at r-project.org
Subject: [R] how to insert NULLs in lists?

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]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list