list.search: Search a list recusively by an expression

Description Usage Arguments Details Examples

View source: R/list.search.R

Description

Search a list recusively by an expression

Usage

1
list.search(.data, expr, classes = "ANY", n, unlist = FALSE)

Arguments

.data

A list or vector

expr

a lambda expression

classes

a character vector of class names that restrict the search. By default, the range is unrestricted (ANY).

n

the maximal number of vectors to return

unlist

logical Should the result be unlisted?

Details

list.search evaluates an expression (expr) recursively along a list (.data).

If the expression results in a single-valued logical vector and its value is TRUE, the whole vector will be collected If it results in multi-valued or non-logical vector, the non-NA values resulted from the expression will be collected.

To search whole vectors that meet certain condition, specify the expression that returns a single logical value.

To search the specific values within the vectors, use subsetting in the expression, that is, .[cond] or lambda expression like x -> x[cond] where cond is a logical vector used to select the elements in the vector.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Exact search

x <- list(p1 = list(type='A',score=c(c1=9)),
       p2 = list(type=c('A','B'),score=c(c1=8,c2=9)),
       p3 = list(type=c('B','C'),score=c(c1=9,c2=7)),
       p4 = list(type=c('B','C'),score=c(c1=8,c2=NA)))

## Search exact values
list.search(x, identical(., 'A'))
list.search(x, identical(., c('A','B')))
list.search(x, identical(., c(9,7)))
list.search(x, identical(., c(c1=9,c2=7)))

## Search all equal values
list.search(x, all(. == 9))
list.search(x, all(. == c(8,9)))
list.search(x, all(. == c(8,9), na.rm = TRUE))

## Search any equal values
list.search(x, any(. == 9))
list.search(x, any(. == c(8,9)))

# Fuzzy search

data <- list(
  p1 = list(name='Ken',age=24),
  p2 = list(name='Kent',age=26),
  p3 = list(name='Sam',age=24),
  p4 = list(name='Keynes',age=30),
  p5 = list(name='Kwen',age=31)
)

list.search(data, grepl('^K\\w+n$', .), 'character')

## Not run: 
library(stringdist)
list.search(data, stringdist(., 'Ken') <= 1, 'character')
list.search(data, stringdist(., 'Man') <= 2, 'character')
list.search(data, stringdist(., 'Man') > 2, 'character')

## End(Not run)

data <- list(
  p1 = list(name=c('Ken', 'Ren'),age=24),
  p2 = list(name=c('Kent', 'Potter'),age=26),
  p3 = list(name=c('Sam', 'Lee'),age=24),
  p4 = list(name=c('Keynes', 'Bond'),age=30),
  p5 = list(name=c('Kwen', 'Hu'),age=31))

list.search(data, .[grepl('e', .)], 'character')

## Not run: 
list.search(data, all(stringdist(., 'Ken') <= 1), 'character')
list.search(data, any(stringdist(., 'Ken') > 1), 'character')

## End(Not run)

Example output

$p1.type
[1] "A"

$p2.type
[1] "A" "B"

named list()
$p3.score
c1 c2 
 9  7 

$p1.score
c1 
 9 

$p2.score
c1 c2 
 8  9 

$p2.score
c1 c2 
 8  9 

$p4.score
c1 c2 
 8 NA 

$p1.score
c1 
 9 

$p2.score
c1 c2 
 8  9 

$p3.score
c1 c2 
 9  7 

$p1.score
c1 
 9 

$p2.score
c1 c2 
 8  9 

$p4.score
c1 c2 
 8 NA 

$p1.name
[1] "Ken"

$p5.name
[1] "Kwen"

sh: 1: wc: Permission denied
sh: 1: cannot create /dev/null: Permission denied
Could not detect number of cores, defaulting to 1.
$p1.name
[1] "Ken"

$p2.name
[1] "Kent"

$p5.name
[1] "Kwen"

$p1.name
[1] "Ken"

$p3.name
[1] "Sam"

$p2.name
[1] "Kent"

$p4.name
[1] "Keynes"

$p5.name
[1] "Kwen"

$p1.name
[1] "Ken" "Ren"

$p2.name
[1] "Kent"   "Potter"

$p3.name
[1] "Lee"

$p4.name
[1] "Keynes"

$p5.name
[1] "Kwen"

$p1.name
[1] "Ken" "Ren"

$p2.name
[1] "Kent"   "Potter"

$p3.name
[1] "Sam" "Lee"

$p4.name
[1] "Keynes" "Bond"  

$p5.name
[1] "Kwen" "Hu"  

rlist documentation built on Sept. 5, 2021, 5:30 p.m.