will be helpful for those in need Borrowing from Spacedman and flodel

here, we can define the following pair of recursive functions:

```
library(tidyverse) # I use dplyr and purrr here, plus tidyr further down below
depth <- function(this) ifelse(is.list(this), 1L + max(sapply(this, depth)), 0L)
bind_at_any_depth <- function(l) {
if (depth(l) == 2) {
return(bind_rows(l))
} else {
l <- at_depth(l, depth(l) - 2, bind_rows)
bind_at_any_depth(l)
}
}
```

```
bind_at_any_depth(x)
```

```
# A tibble: 2 × 2
a b
<dbl> <dbl>
1 1 2
2 3 4
```

```
bind_at_any_depth(x_ext) # From P Lapointe
```

```
# A tibble: 5 × 2
a b
<dbl> <dbl>
1 1 2
2 5 6
3 7 8
4 1 2
5 3 4
```

```
bind_at_any_depth2 <- function(l) {
if (depth(l) == 2) {
l <- bind_rows(l, .id = 'source')
l <- unite(l, 'source', contains('source'))
return(l)
} else {
l <- at_depth(l, depth(l) - 2, bind_rows, .id = paste0('source', depth(l)))
bind_at_any_depth(l)
}
}
```

```
bind_at_any_depth2(x_ext)
```

```
# A tibble: 5 × 3
source a b
* <chr> <dbl> <dbl>
1 X_x_1 1 2
2 X_y_z 5 6
3 X_y_zz 7 8
4 Y_x_1 1 2
5 Y_y_1 3 4
```