Skip to content

Remove empty list from list

7 messages · Gabor Grothendieck, jim holtman, Muhammad Subianto +1 more

#
Dear all,
I am still working with "list".
If I have an empty list how can I remove from list data.
Here is a toy example:
x <- list(matrix(1:20, 5, 4),matrix(1:20, 5, 4),matrix(1:20, 5,
4),matrix(1:20, 5, 4),matrix(1:20, 5, 4))
y <- list(c(1, -1, -1, 1, 1),c(1, 1, -1, -1, -1),c(1, 1, 1, 1, 1),c(1,
1, -1, 1, -1),c(-1, -1, -1, -1, -1))
## Thanks to Gabor Grothendieck for this trick.
## SIMPLIFY? SIMPLIFY >< simplify
xy.list <- mapply(cbind, x, y, SIMPLIFY=FALSE)

point.class <- t(cbind(c(10,20,15,4,-1),c(21,10,15,34,-1),c(11,13,6,3,1),c(7,5,5,2,1),c(8,9,5,12,-1)))
class.diffsame <- points.neighb(as.matrix(point.class), xy.list, 5)
pd.class <- points.diff(class.diffsame,xy.list)

nc.test <- vector("list",length(pd.class))
for (i in 1:length(pd.class)) {
     nc.test[[i]] <- pd.class[[i]]$point.diff
}
nc.test
[[1]]
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    6   11   16    1
[2,]    4    9   14   19    1
[3,]    5   10   15   20    1

[[2]]
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    6   11   16    1
[2,]    2    7   12   17    1

[[3]]
     [,1] [,2] [,3] [,4] [,5]

[[4]]
     [,1] [,2] [,3] [,4] [,5]
[1,]    3    8   13   18   -1
[2,]    5   10   15   20   -1

[[5]]
     [,1] [,2] [,3] [,4] [,5]
I want to remove these:
nc.test[[3]]
nc.test[[5]]
Because my list data have more 1000 lists are there any simple way to do this?

Best, Muhammad Subianto


points.neighb <- function(p.class, list.nc, class.col) {
   ntuples <- nrow(p.class)
   instvec <- vector("list",length=ntuples)
   for (i in 1:ntuples) {
        # Thanks to Petr Pikal for this trick
        instvec[[i]]$class.diff <- (p.class[i,class.col] -
list.nc[[i]][,class.col])!=0
        instvec[[i]]$class.same <- (p.class[i,class.col] -
list.nc[[i]][,class.col])==0
   }
   instvec
}

points.diff <- function(p.class, list.nc) {
   ntuples <- length(list.nc)
   instvec <- vector("list",ntuples)
   for (i in 1:ntuples) {
        instvec[[i]]$point.diff <- list.nc[[i]][p.class[[i]]$class.diff,]
        instvec[[i]]$point.same <- list.nc[[i]][p.class[[i]]$class.same,]
   }
   instvec
}
#
See this post from the weekend:
http://www.nabble.com/Re%3A-How-to-iteratively-extract-elements-out-of-a-list-p6002980.html
On 8/28/06, Muhammad Subianto <msubianto at gmail.com> wrote:
#
Here is a function I use a lot to do this:

delete.NULLs  <-  function(x.list){   # delele null/empty entries in a list
    x.list[unlist(lapply(x.list, length) != 0)]
}
[[1]]
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    6   11   16    1
[2,]    4    9   14   19    1
[3,]    5   10   15   20    1

[[2]]
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    6   11   16    1
[2,]    2    7   12   17    1

[[3]]
     [,1] [,2] [,3] [,4] [,5]
[1,]    3    8   13   18   -1
[2,]    5   10   15   20   -1
On 8/28/06, Muhammad Subianto <msubianto at gmail.com> wrote:

  
    
#
On this day 28/08/2006 19:20, Muhammad Subianto wrote:
Dear
Jim Holtman and Gabor Grothendieck,
Thank you both very much for the suggestions!
These is exactly what I was looking for.

Best wishes, Muhammad Subianto


## delete null/empty entries in a list
delete.NULLs  <-  function(x.list){
     x.list[unlist(lapply(x.list, length) != 0)]
}

 > delete.NULLs  <-  function(x.list){
+     x.list[unlist(lapply(x.list, length) != 0)]
+ }
 > delete.NULLs  <-  function(x.list){
+     x.list[unlist(lapply(x.list, length) != 0)]
+ }
 >
 > delete.NULLs(nc.test)
[[1]]
      [,1] [,2] [,3] [,4] [,5]
[1,]    1    6   11   16    1
[2,]    4    9   14   19    1
[3,]    5   10   15   20    1

[[2]]
      [,1] [,2] [,3] [,4] [,5]
[1,]    1    6   11   16    1
[2,]    2    7   12   17    1

[[3]]
      [,1] [,2] [,3] [,4] [,5]
[1,]    3    8   13   18   -1
[2,]    5   10   15   20   -1

 >
#
Dear all,
After I work around, I found in my list of data with only one row which 
need to remove or make it as matrix.
Here I write again my other toy example:
x <- list(matrix(1:20, 5, 4),matrix(1:20, 5, 4),matrix(1:20, 5, 
4),matrix(1:20, 5, 4),matrix(1:20, 5, 4))
y <- list(c(1, -1, -1, 1, 1),c(1, -1, -1, -1, -1),c(1, 1, 1, 1, 1),c(1, 
1, -1, 1, -1),c(-1, -1, -1, -1, -1))
## Thanks to Gabor Grothendieck for this trick.
## SIMPLIFY? SIMPLIFY >< simplify
xy.list <- mapply(cbind, x, y, SIMPLIFY=FALSE)

point.class <- 
t(cbind(c(10,20,15,4,-1),c(21,10,15,34,-1),c(11,13,6,3,1),c(7,5,5,2,1),c(8,9,5,12,-1)))
class.diffsame <- points.neighb(as.matrix(point.class), xy.list, 5)
pd.class <- points.diff(class.diffsame,xy.list)

nc.test <- vector("list",length(pd.class))
for (i in 1:length(pd.class)) {
      nc.test[[i]] <- pd.class[[i]]$point.diff
}
nc.test

# delete null/empty entries in a list
dff <- delete.NULLs(nc.test)
dff; str(dff)
 > dff
[[1]]
      [,1] [,2] [,3] [,4] [,5]
[1,]    1    6   11   16    1
[2,]    4    9   14   19    1
[3,]    5   10   15   20    1

[[2]]
[1]  1  6 11 16  1

[[3]]
      [,1] [,2] [,3] [,4] [,5]
[1,]    3    8   13   18   -1
[2,]    5   10   15   20   -1

 >

lapply(dff, nrow)
 > lapply(dff, nrow)
[[1]]
[1] 3

[[2]]
NULL

[[3]]
[1] 2

 >

#I can use
#dff[unlist(lapply(dff, nrow) == 1)] #2,3, etc

I have two questions here:
a. I need to remove dff[[2]]
b. How to make it as matrix (in list). I mean the result something like

[[1]]
      [,1] [,2] [,3] [,4] [,5]
[1,]    1    6   11   16    1
[2,]    4    9   14   19    1
[3,]    5   10   15   20    1

[[2]]
      [,1] [,2] [,3] [,4] [,5]
[1]    1    6    11   16    1

[[3]]
      [,1] [,2] [,3] [,4] [,5]
[1,]    3    8   13   18   -1
[2,]    5   10   15   20   -1


Best, Muhammad Subianto


points.neighb <- function(p.class, list.nc, class.col) {
    ntuples <- nrow(p.class)
    instvec <- vector("list",length=ntuples)
    for (i in 1:ntuples) {
         # Thanks to Petr Pikal for this trick
         instvec[[i]]$class.diff <- (p.class[i,class.col] - 
list.nc[[i]][,class.col])!=0
         instvec[[i]]$class.same <- (p.class[i,class.col] - 
list.nc[[i]][,class.col])==0
    }
    instvec
}

points.diff <- function(p.class, list.nc) {
    ntuples <- length(list.nc)
    instvec <- vector("list",ntuples)
    for (i in 1:ntuples) {
         instvec[[i]]$point.diff <- list.nc[[i]][p.class[[i]]$class.diff,]
         instvec[[i]]$point.same <- list.nc[[i]][p.class[[i]]$class.same,]
    }
    instvec
}

# Thanks to Jim Holtman for this trick
delete.NULLs  <-  function(x.list){
     x.list[unlist(lapply(x.list, length) != 0)]
}
#
try the following:

# a
dff[sapply(dff, is.matrix)]

# b
lapply(dff, function(x) if(!is.matrix(x)) rbind(x) else x)


I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "Muhammad Subianto" <msubianto at gmail.com>
To: <r-help at stat.math.ethz.ch>
Sent: Tuesday, August 29, 2006 1:22 PM
Subject: Re: [R] Remove empty list from list - remove only one row and 
make as matrix
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
#
Dear all,
Dimitris, thanks for your great help and quick response.

Best, Muhammad Subianto


 > dff[sapply(dff, is.matrix)]
[[1]]
      [,1] [,2] [,3] [,4] [,5]
[1,]    1    6   11   16    1
[2,]    4    9   14   19    1
[3,]    5   10   15   20    1

[[2]]
      [,1] [,2] [,3] [,4] [,5]
[1,]    3    8   13   18   -1
[2,]    5   10   15   20   -1

 > lapply(dff, function(x) if(!is.matrix(x)) rbind(x) else x)
[[1]]
      [,1] [,2] [,3] [,4] [,5]
[1,]    1    6   11   16    1
[2,]    4    9   14   19    1
[3,]    5   10   15   20    1

[[2]]
   [,1] [,2] [,3] [,4] [,5]
x    1    6   11   16    1

[[3]]
      [,1] [,2] [,3] [,4] [,5]
[1,]    3    8   13   18   -1
[2,]    5   10   15   20   -1

 >
On this day 29/08/2006 13:34, Dimitris Rizopoulos wrote: