sublist: Extract a sublist (or subset) of a list.

View source: R/list_fun.R

sublistR Documentation

Extract a sublist (or subset) of a list.

Description

sublist yields a sublist of a list ls by filtering or extracting a subset of the list's dimensions and levels.

Usage

sublist(ls, in_list = ls, out_list = NULL, quiet = FALSE)

Arguments

ls

The original list (as list).

in_list

A list specifying the tag and value(s) of ls to keep/include. Default: in_list = ls (i.e., everything).

out_list

A list specifying the tag and value(s) of ls to drop/exclude. Default: out_list = NULL (i.e., nothing).

quiet

Boolean: Hide feedback messages? Default: quiet = FALSE (i.e., show messages).

Details

sublist provides a filter/slice function for lists, by specifying a positive subset in_list and a negative subset out_list (both as lists, in tag and value format).

Value

A list.

See Also

subtable for extracting subsets of a table.

Examples

(ls <- list(n = 1:4, l = letters[1:4]))

# Trivial cases:
sublist(ls, in_list = ls)
sublist(ls, out_list = ls)

# (a) in_list only:
sublist(ls,  in_list = list(l = letters[c(2, 4)], n = c(2, 4)))
sublist(ls,  in_list = list(n = 4))

# NA/empty cases:
sublist(ls,  in_list = NA)      # returns NA
sublist(ls,  in_list = list())  # returns an empty list

# Note:
sublist(ls,  in_list = list(l = 4, n = 4))      # matching list elements and levels
sublist(ls,  in_list = list(l = 99, n = 99))    # non-existent levels: nothing in
sublist(ls,  in_list = list(ll = 99, nn = 99))  # heuristics match list elements

# (b) out_list only:
sublist(ls, out_list = list(l = letters[c(2, 4)], n = c(2, 4)))
sublist(ls, out_list = list(n = 3, l = "c"))

# NA/empty cases:
sublist(ls, out_list = NA)      # returns NA
sublist(ls, out_list = list())  # returns original list

# Note:
sublist(ls, out_list = list(l = 4, n = 4))      # matching list elements and levels
sublist(ls, out_list = list(l = 99, n = 99))    # non-existent levels: nothing out
sublist(ls, out_list = list(ll = 99, nn = 99))  # heuristics match list elements

# (c) in_list AND out_list:
sublist(ls, in_list = list(n = 3:4, l = c("c", "a")), out_list = list(l = "c"))
sublist(ls, in_list = list(n = 3:4, l = c("c", "a")), out_list = list(n = 4, l = "c"))

# removing everything:
sublist(ls, in_list = list(n = 4:3), out_list = list(n = 3:4))
sublist(ls, in_list = list(l = c("c", "a")), out_list = list(l = c("a", "c")))
sublist(ls, in_list = list(n = 3:4, l = c("a", "c")),
        out_list = list(n = 4:3, l = c("c", "a")))

# Note: Tags can be used repeatedly and empty list elements are dropped:
sublist(ls, out_list = list(l = c("c", "b"), n = 2:3))
sublist(ls, out_list = list(l = c("c", "b"), n = 2:3, l = c("a", "d")))
sublist(ls, out_list = list(l = c("c", "b"), n = 2:3, l = c("a", "d"), n = 1:4))


hneth/i2ds documentation built on Aug. 17, 2024, 9:31 p.m.