Gonçalo Ferraz
2012-Nov-15 21:01 UTC
[R] assigning NULL to a list element without changing the length of the list
Hi, I have a list: tmp0 <- list(a=1, b=2, c=3) And I realize that I can append a NULL element to the end of this list, just by writing: length(tmp0) <- 4 Now, the question is, how can I assign NULL to one of the existing list elements without changing the length of the list? Please note I am working from inside a for loop that is working on one list element at a time and in some circumstances I want to set one element to NULL. So, specifying the whole list again as in: tmp0 <- list(a=1,b=NULL,c=3) is not an option. But writing, say: tmp0 [[2]] <- NULL is not an option either, because it leaves me with a list of length 2. Is there a solution for this? Thank you
William Dunlap
2012-Nov-15 21:09 UTC
[R] assigning NULL to a list element without changing the length of the list
tmp0 <- list(a=1, b=2, c=3) tmp0["b"] <- list(NULL) # single [, list(NULL), not double [[ and bare NULL str(tmp0) # List of 3 # $ a: num 1 # $ b: NULL # $ c: num 3 Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf > Of Gon?alo Ferraz > Sent: Thursday, November 15, 2012 1:01 PM > To: r-help at r-project.org > Subject: [R] assigning NULL to a list element without changing the length of the list > > Hi, > > I have a list: > > tmp0 <- list(a=1, b=2, c=3) > > And I realize that I can append a NULL element to the end of this list, just by writing: > > length(tmp0) <- 4 > > Now, the question is, how can I assign NULL to one of the existing list elements without > changing the length of the list? > > Please note I am working from inside a for loop that is working on one list element at a > time and in some circumstances I want to > set one element to NULL. So, specifying the whole list again as in: > > tmp0 <- list(a=1,b=NULL,c=3) > > is not an option. > > But writing, say: > > tmp0 [[2]] <- NULL > > is not an option either, because it leaves me with a list of length 2. > > Is there a solution for this? > > Thank you > > ______________________________________________ > 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.
arun
2012-Nov-15 23:48 UTC
[R] assigning NULL to a list element without changing the length of the list
HI,
You could try these:
tmp1<-list(a=1,b=NA,c=3,d=NA)
?lapply(tmp1,function(x) if(is.na(x)) NULL else x)? #changing NA to NULL
#$a
#[1] 1
#
#$b
#NULL
#
#$c
#[1] 3
#
#$d
#NULL
#other case;
tmp2<-list(a=1,b=3,c=4,d=5)? #want to change list elements "a",
"c" to NULL
tmp3<-lapply(tmp2,function(x) {if(names(tmp2)[match.call()[[2]][[3]]]%in%
c("a","c")) NULL else x})
?tmp3
#$a
#NULL
#
#$b
#[1] 3
#
#$c
#NULL
#
#$d
#[1] 5
A.K.
----- Original Message -----
From: Gon?alo Ferraz <gferraz29 at gmail.com>
To: r-help at r-project.org
Cc:
Sent: Thursday, November 15, 2012 4:01 PM
Subject: [R] assigning NULL to a list element without changing the length of the
list
Hi,
I have a list:
tmp0 <- list(a=1, b=2, c=3)
And I realize that I can append a NULL element to the end of this list, just by
writing:
length(tmp0) <- 4
Now, the question is, how can I assign NULL to one of the existing list elements
without changing the length of the list?
Please note I am working from inside a for loop that is working on one list
element at a time and in some circumstances I want to
set one element to NULL. So, specifying the whole list again as in:
tmp0 <- list(a=1,b=NULL,c=3)
is not an option.
But writing, say:
tmp0 [[2]] <- NULL
is not an option either, because it leaves me with a list of length 2.
Is there a solution for this?
Thank you
______________________________________________
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.