Muhammad Azam
2008-Oct-15 10:23 UTC
[R] request: How can we ignore a component of list having no element
Dear friends
There is a list of arrays comprising different no of rows and columns even
sometimes NULL, such as [[2]] given below. How can we ignore [[2]] or others
like this in the complete list. Any help in this regard is needed. Thanks
[[1]]
[,1] [,2]
[1,] 3 1
[2,] 3 1
[3,] 3 1
[[2]]
NULL
[[3]]
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] 3 1 0 0 0 0 0
[2,] 3 1 0 0 0 0 0
[3,] 3 1 0 0 0 0 0
[4,] 3 1 3 1 3 2 1
[5,] 3 1 3 1 3 2 1
[6,] 3 1 3 1 3 2 0
[[4]]
[,1] [,2] [,3] [,4]
[1,] 3 0 0 0
[2,] 3 1 3 3
[3,] 3 1 3 3
[4,] 3 1 3 0
OR
x1=c(1,2,3); x2=c(1,2,3,4,6); x3=c(); x=list(x1,x2,x3)
M.Azam
[[alternative HTML version deleted]]
Muhammad Azam
2008-Oct-15 10:53 UTC
[R] request: How can we ignore a component of list having no element
Dear Mahbub
Thanks a lot for the help.
----- Original Message ----
From: Mahbub Latif <mahbub.latif@gmail.com>
To: Muhammad Azam <mazam72@yahoo.com>
Sent: Wednesday, October 15, 2008 12:30:13 PM
Subject: Re: [R] request: How can we ignore a component of list having no
element
try this,
junk <- sapply(x,function(i) !is.null(i))
y <- x[junk]
On Wed, Oct 15, 2008 at 11:23 AM, Muhammad Azam <mazam72@yahoo.com> wrote:
Dear friends
There is a list of arrays comprising different no of rows and columns even
sometimes NULL, such as [[2]] given below. How can we ignore [[2]] or others
like this in the complete list. Any help in this regard is needed. Thanks
[[1]]
[,1] [,2]
[1,] 3 1
[2,] 3 1
[3,] 3 1
[[2]]
NULL
[[3]]
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] 3 1 0 0 0 0 0
[2,] 3 1 0 0 0 0 0
[3,] 3 1 0 0 0 0 0
[4,] 3 1 3 1 3 2 1
[5,] 3 1 3 1 3 2 1
[6,] 3 1 3 1 3 2 0
[[4]]
[,1] [,2] [,3] [,4]
[1,] 3 0 0 0
[2,] 3 1 3 3
[3,] 3 1 3 3
[4,] 3 1 3 0
OR
x1=c(1,2,3); x2=c(1,2,3,4,6); x3=c(); x=list(x1,x2,x3)
M.Azam
[[alternative HTML version deleted]]
______________________________________________
R-help@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.
--
A.H.M. Mahbub Latif
Assistant Professor
Applied Statistics
Institute of Statistical Research and Training
University of Dhaka, Dhaka 1000, Bangladesh
web : http://www.isrt.ac.bd/mlatif
[[alternative HTML version deleted]]
hadley wickham
2008-Oct-15 12:38 UTC
[R] request: How can we ignore a component of list having no element
An alternative approach would be to store 0 x 0 matrices instead of NULLs. This way every object in your list is a consistent type. Hadley On Wed, Oct 15, 2008 at 5:23 AM, Muhammad Azam <mazam72 at yahoo.com> wrote:> Dear friends > There is a list of arrays comprising different no of rows and columns even sometimes NULL, such as [[2]] given below. How can we ignore [[2]] or others like this in the complete list. Any help in this regard is needed. Thanks > > [[1]] > [,1] [,2] > [1,] 3 1 > [2,] 3 1 > [3,] 3 1 > > [[2]] > NULL > > [[3]] > [,1] [,2] [,3] [,4] [,5] [,6] [,7] > [1,] 3 1 0 0 0 0 0 > [2,] 3 1 0 0 0 0 0 > [3,] 3 1 0 0 0 0 0 > [4,] 3 1 3 1 3 2 1 > [5,] 3 1 3 1 3 2 1 > [6,] 3 1 3 1 3 2 0 > > [[4]] > [,1] [,2] [,3] [,4] > [1,] 3 0 0 0 > [2,] 3 1 3 3 > [3,] 3 1 3 3 > [4,] 3 1 3 0 > > OR > x1=c(1,2,3); x2=c(1,2,3,4,6); x3=c(); x=list(x1,x2,x3) > > M.Azam > > > > [[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. >-- http://had.co.nz/
Adaikalavan Ramasamy
2008-Oct-15 14:47 UTC
[R] request: How can we ignore a component of list having no element
Try x[ !sapply(x, is.null) ] hadley wickham wrote:> An alternative approach would be to store 0 x 0 matrices instead of > NULLs. This way every object in your list is a consistent type. > > Hadley > > On Wed, Oct 15, 2008 at 5:23 AM, Muhammad Azam <mazam72 at yahoo.com> wrote: >> Dear friends >> There is a list of arrays comprising different no of rows and columns even sometimes NULL, such as [[2]] given below. How can we ignore [[2]] or others like this in the complete list. Any help in this regard is needed. Thanks >> >> [[1]] >> [,1] [,2] >> [1,] 3 1 >> [2,] 3 1 >> [3,] 3 1 >> >> [[2]] >> NULL >> >> [[3]] >> [,1] [,2] [,3] [,4] [,5] [,6] [,7] >> [1,] 3 1 0 0 0 0 0 >> [2,] 3 1 0 0 0 0 0 >> [3,] 3 1 0 0 0 0 0 >> [4,] 3 1 3 1 3 2 1 >> [5,] 3 1 3 1 3 2 1 >> [6,] 3 1 3 1 3 2 0 >> >> [[4]] >> [,1] [,2] [,3] [,4] >> [1,] 3 0 0 0 >> [2,] 3 1 3 3 >> [3,] 3 1 3 3 >> [4,] 3 1 3 0 >> >> OR >> x1=c(1,2,3); x2=c(1,2,3,4,6); x3=c(); x=list(x1,x2,x3) >> >> M.Azam >> >> >> >> [[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. >> > > >