[R] detecting NULL in recursive lists

Charles C. Berry cberry at tajo.ucsd.edu
Tue Mar 10 00:56:53 CET 2009


On Mon, 9 Mar 2009, Vadim Ogranovich wrote:

> Dear R-users,
>
> How can I detect a NULL in a recursive list?
>
> For a regular list I could use lapply:
>
>> lapply(list(x=NULL), is.null)
> $x
> [1] TRUE
>
>
> However that doesn't work for structures like list(list(x=NULL)). I tried rapply but it treats NULL as a list and discards them:
>
>> rapply(list(a=1, b=list(x=NULL)), is.null)
>    a
> FALSE
>
> Any suggestion?

Is this what you want?

> foo <- function(x) sapply(x, function(x) if (is.list(x)) foo(x) else length(x)==0,simplify=FALSE)
> foo(list(a=list(A=1:2,B=NULL), b=list(C=NULL,D=1)))
$a
$a$A
[1] FALSE

$a$B
[1] TRUE


$b
$b$C
[1] TRUE

$b$D
[1] FALSE


> 
>

HTH,

Chuck

>
> Thank you for your help,
> Vadim
>
> Note: This email is for the confidential use of the named addressee(s) only and may contain proprietary, confidential or privileged information. If you are not the intended recipient, you are hereby notified that any review, dissemination or copying of this email is strictly prohibited, and to please notify the sender immediately and destroy this email and any attachments.  Email transmission cannot be guaranteed to be secure or error-free.  Jump Trading, therefore, does not make any guarantees as to the completeness or accuracy of this email or any attachments.  This email is for informational purposes only and does not constitute a recommendation, offer, request or solicitation of any kind to buy, sell, subscribe, redeem or perform any type of transaction of a financial product.
>
> ______________________________________________
> 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.
>

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901




More information about the R-help mailing list