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 |
predicate |
A |
bare |
Should algorithm only continue for bare lists? Defaults to TRUE. See |
... |
Additional arguments to pass to |
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
-
depths()
returns aninteger
vector indicating the levels that contain elements satisfying the predicate. -
depths_string()
returns acharacter
representation of the traversal. Brackets {} are used to indicate the level of the tree, commas to separate element-indices within a level, and the sign of the index to indicate whether the element satisfiedpredicate
(- = yes, + = no).
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
)