Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a list is nested or not

Is there a "built-in"/efficient and robust way to check if list objects are nested or not?

To clarify my understanding of the term nested:

Flat or not-nested list

x.1 <- list(
    a=TRUE, 
    b=1:5
)

Nested list

x.2 <- list(
    a=list(a.1=list(a.1.1=TRUE)), 
    b=list(b.1=1:5)
)

My first idea was to use a combination of str, capture.output and regular expressions. But as everything related to regular expression: pretty powerful, pretty risky on the robustness side ;-) So I wondered if there's something better out there:

isNested <- function(x) {
    if (class(x) != "list") {
        stop("Expecting 'x' to be a list")
    }
    out <- FALSE
    strout <- capture.output(str(x))
    idx <- grep("\\$.*List", strout)
    if (length(idx)) {
        out <- TRUE
    }
    return(out)
}

> isNested(x=x.1)
[1] FALSE
> isNested(x=x.2)
[1] TRUE

Second approach courtesy of Roman and Arun:

isNested2 <- function(x) {
    if (class(x) != "list") {
        stop("Expecting 'x' to be a list")
    }
    out <- any(sapply(x, is.list))
    return(out)
}

> isNested2(x=x.1)
[1] FALSE
> isNested2(x=x.2)
[1] TRUE
like image 626
Rappster Avatar asked Mar 13 '13 10:03

Rappster


People also ask

Is nested list?

A nested list is a list of lists, or any list that has another list as an element (a sublist). They can be helpful if you want to create a matrix or need to store a sublist along with other data types.

How do you check if a sublist is in a list?

issubset() function. The most used and recommended method to check for a sublist. This function is tailor made to perform the particular task of checking if one list is a subset of another.

Is Empty list a nested list?

A list within another list is said to be nested. Finally, a list with no elements is called an empty list, and is denoted [].


2 Answers

You can use the is.list function:

any(sapply(x.1, is.list))
[1] FALSE

any(sapply(x.2, is.list))
[1] TRUE

As a function isNested:

isNested <- function(l) {
  stopifnot(is.list(l))
  for (i in l) {
    if (is.list(i)) return(TRUE)
  }
  return(FALSE)
}

Instead of testing all list elements, the function stops as soon as it detects a nested list.

like image 132
Sven Hohenstein Avatar answered Sep 20 '22 00:09

Sven Hohenstein


Try this :

   isNested <- function(x) {
    if (is.list(x))
        stop("Expecting 'x' to be a list")

    any(unlist( lapply(x,is.list) ))
   }
like image 24
Pop Avatar answered Sep 20 '22 00:09

Pop