Skip to content

how to delete the null elements in list

4 messages · Tammy Ma, Rui Barradas, Ben Bolker +1 more

#
Hello,

Try the following.

suu <- list(NULL, NULL, 1:2, matrix(1:4, 2))
suu[!sapply(suu, is.null)]


Hope this helps,

Rui Barradas


Em 25-01-2013 12:31, Tammy Ma escreveu:
#
Tammy Ma <metal_licaling <at> live.com> writes:
How about suu[!sapply(suu,is.null)] ?  (Haven't tested, but I would
expect it to work ...

  Of course, the result won't have elements [[3]] and [[4]],
it will have elements [[1]] and [[2]] ...

 Ben Bolker
#
Or,
suu[unlist(lapply(suu,length)!=0)]
#[[1]]
#[1] 1 2

#[[2]]
?# ?? [,1] [,2]
#[1,]??? 1??? 3
#[2,]??? 2??? 4
A.K.




----- Original Message -----
From: Rui Barradas <ruipbarradas at sapo.pt>
To: Tammy Ma <metal_licaling at live.com>
Cc: "r-help at r-project.org" <r-help at r-project.org>
Sent: Friday, January 25, 2013 8:15 AM
Subject: Re: [R] how to delete the null elements in list

Hello,

Try the following.

suu <- list(NULL, NULL, 1:2, matrix(1:4, 2))
suu[!sapply(suu, is.null)]


Hope this helps,

Rui Barradas


Em 25-01-2013 12:31, Tammy Ma escreveu:
______________________________________________
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.