12

I have a nested element like this

> x <- list(a=list(from="me", id="xyz"), b=list(comment=list(list(message="blabla", id="abc"), list(message="humbug", id="jkl"))), id="123")
> str(x)
List of 3
 $ a :List of 2
  ..$ from: chr "me"
  ..$ id  : chr "xyz"
 $ b :List of 1
  ..$ comment:List of 2
  .. ..$ :List of 2
  .. .. ..$ message: chr "blabla"
  .. .. ..$ id     : chr "abc"
  .. ..$ :List of 2
  .. .. ..$ message: chr "humbug"
  .. .. ..$ id     : chr "jkl"
 $ id: chr "123"

How can I remove all the elements with name id in all levels of the list? i.e. the expected output is

> str(x)
List of 2
 $ a:List of 1
  ..$ from: chr "me"
 $ b:List of 1
  ..$ comment:List of 2
  .. ..$ :List of 1
  .. .. ..$ message: chr "blabla"
  .. ..$ :List of 1
  .. .. ..$ message: chr "humbug"

Solutions using rlist package would be particularly welcome, but I'm happy with anything that works.

Ricky
  • 4,616
  • 6
  • 42
  • 72

3 Answers3

10

Recursion is also how I did it:

# recursive function to remove name from all levels of list
stripname <- function(x, name) {
    thisdepth <- depth(x)
    if (thisdepth == 0) {
        return(x)
    } else if (length(nameIndex <- which(names(x) == name))) {
        x <- x[-nameIndex]
    }
    return(lapply(x, stripname, name))
}

# function to find depth of a list element
# see http://stackoverflow.com/questions/13432863/determine-level-of-nesting-in-r
depth <- function(this, thisdepth=0){
    if (!is.list(this)) {
        return(thisdepth)
    } else{
        return(max(unlist(lapply(this,depth,thisdepth=thisdepth+1))))    
    }
}


str(stripname(x, "id"))
## List of 2
## $ a:List of 1
## ..$ from: chr "me"
## $ b:List of 1
## ..$ comment:List of 2
## .. ..$ :List of 1
## .. ..$ :List of 1
## .. .. ..$ message: chr "blabla"
## .. .. ..$ message: chr "humbug"
Ken Benoit
  • 14,454
  • 27
  • 50
  • You could replace `if (length(nameIndex <- which(names(x) == name)))` by `if (any(nameIndex <- names(x) == name)))` I think – Julien Jul 06 '23 at 08:02
3

Try a recursive function in the veins of

f <- function(i) 
  lapply(i, function(x) 
    if (is.list(x)) { 
      if(!is.null(names(x))) f(x[names(x)!="id"]) else f(x) 
    } else x
  )
str(f(x[names(x)!="id"]))
# List of 2
#  $ a:List of 1
#   ..$ from: chr "me"
#  $ b:List of 1
#   ..$ comment:List of 2
#   .. ..$ :List of 1
#   .. .. ..$ message: chr "blabla"
#   .. ..$ :List of 1
#   .. .. ..$ message: chr "humbug"
lukeA
  • 53,097
  • 5
  • 97
  • 100
  • This will only work if the depth of the list is 2 as in the example. If there are deeper elements, we need a recursive stuff -- maybe rewrite with `rapply`(didn't succeed) – Eric Lecoutre Jun 16 '16 at 09:23
2

This is an old question, but this can also be done quite conveniently with rrapply() in the rrapply-package (revisit of base rapply()):

rrapply::rrapply(
  x,                                        ## nested list
  condition = \(x, .xname) .xname != "id",  ## filter condition
  how = "prune"                             ## how to structure result
) |>
  str()

#> List of 2
#>  $ a:List of 1
#>   ..$ from: chr "me"
#>  $ b:List of 1
#>   ..$ comment:List of 2
#>   .. ..$ :List of 1
#>   .. .. ..$ message: chr "blabla"
#>   .. ..$ :List of 1
#>   .. .. ..$ message: chr "humbug"
Joris C.
  • 5,721
  • 3
  • 12
  • 27