0

I would like to apply a function across all columns, which are nested data frames or tibbles.

What would be the best way to check if a column is a nested data frame (for instance to be used with mutate_if or across? Here is an example nested data frame, where it should work with.

   (mpg %>% group_by(manufacturer) %>% nest())$manufacturer
    (mpg %>% group_by(manufacturer) %>% nest())$data
Quinten
  • 35,235
  • 5
  • 20
  • 53
Saren Tasciyan
  • 454
  • 4
  • 15
  • 1
    Please, provide a minimal reproducible example: [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – PaulS Jul 18 '22 at 21:24
  • How about `is.recursive()`? – MorganK95 Jul 19 '22 at 04:56

1 Answers1

0

A very crude and simple approach I came up with is the following:

library(tidyr)
library(dplyr)
#' Checks whether or not a column is a nested data frame
#'
#' @param col column
#'
#' @return TRUE if it is a nested data frame (List of data frames), otherwise FALSE
#' @export
#'
#' @examples
is.nested.data.frame = function(col){
    if(is.list(col)){
        if(length(col) > 0){ # We need to be careful about empty lists for the next step
            if(is.data.frame(col[[1]])){
                return(TRUE)
            }
        }
    }
    return(FALSE)
}

is.nested.data.frame((mpg %>% group_by(manufacturer) %>% nest())$manufacturer)
is.nested.data.frame((mpg %>% group_by(manufacturer) %>% nest())$data)



df = mpg %>% group_by(manufacturer) %>% nest()
df
df %>% mutate(across(where(is.nested.data.frame), length))
Saren Tasciyan
  • 454
  • 4
  • 15