depths {cheese}R Documentation

Find the elements in a list structure that satisfy a predicate

Description

Traverse a list of structure to find the depths and positions of its elements that satisfy a predicate.

Usage

depths(
    list,
    predicate,
    bare = TRUE,
    ...
)
depths_string(
    list,
    predicate,
    bare = TRUE,
    ...
)

Arguments

list

A list, data.frame, or vector.

predicate

A function that evaluates to TRUE or FALSE.

bare

Should algorithm only continue for bare lists? Defaults to TRUE. See rlang::`bare-type-predicates`

...

Additional arguments to pass to predicate.

Details

The input is recursively evaluated to find elements that satisfy predicate, and only proceeds where rlang::is_list when argument bare is FALSE, and rlang::is_bare_list when it is TRUE.

Value

Author(s)

Alex Zajichek

Examples

#Find depths of data frames
df1 <-
  heart_disease %>%
  
    #Divide the frame into a list
    divide(
      Sex,
      HeartDisease,
      ChestPain
    )

df1 %>%
  
  #Get depths as an integer
  depths(
    predicate = is.data.frame
  )

df1 %>%

  #Get full structure
  depths_string(
    predicate = is.data.frame
  )

#Shallower list
df2 <-
  heart_disease %>%
    divide(
      Sex,
      HeartDisease,
      ChestPain,
      depth = 1
    ) 

df2 %>%
  depths(
    predicate = is.data.frame
  )

df2 %>%
  depths_string(
    predicate = is.data.frame
  )

#Allow for non-bare lists to be traversed
df1 %>%
  depths(
    predicate = is.factor,
    bare = FALSE
  )

#Make uneven list with diverse objects
my_list <-
  list(
    heart_disease,
    list(
      heart_disease
    ),
    1:10,
    list(
      heart_disease$Age,
      list(
        heart_disease
      )
    ),
    glm(
      formula = HeartDisease ~ .,
      data = heart_disease,
      family = "binomial"
    )
  )

#Find the data frames
my_list %>%
  depths(
    predicate = is.data.frame
  )

my_list %>%
  depths_string(
    predicate = is.data.frame
  )

#Go deeper by relaxing bare list argument
my_list %>%
  depths_string(
    predicate = is.data.frame,
    bare = FALSE
  )


[Package cheese version 0.1.2 Index]