Safely extracts elements from a list, returning NA if the extraction fails
Usage
safe_extract(list, field, index = 1)
Arguments
- list
A list object from which to extract elements
- field
The name of the field to extract from the list
- index
The index position to extract from the field. Default is 1
Value
A single character value if successful, otherwise
NA_character_ when the field is absent, empty, or shorter than
index.
Examples
# Create a sample list
my_list <- list(
a = list(x = 1:3),
b = list(y = 4:6)
)
# Extract existing element
safe_extract(my_list, "a", 1)
# Extract non-existing element (returns NA)
safe_extract(my_list, "c", 1)