EVOLUTION-MANAGER
Edit File: list.search.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Search a list recusively by an expression</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for list.search {rlist}"><tr><td>list.search {rlist}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Search a list recusively by an expression</h2> <h3>Description</h3> <p>Search a list recusively by an expression </p> <h3>Usage</h3> <pre> list.search(.data, expr, classes = "ANY", n, unlist = FALSE) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>.data</code></td> <td> <p>A <code>list</code> or <code>vector</code></p> </td></tr> <tr valign="top"><td><code>expr</code></td> <td> <p>a lambda expression</p> </td></tr> <tr valign="top"><td><code>classes</code></td> <td> <p>a character vector of class names that restrict the search. By default, the range is unrestricted (<code>ANY</code>).</p> </td></tr> <tr valign="top"><td><code>n</code></td> <td> <p>the maximal number of vectors to return</p> </td></tr> <tr valign="top"><td><code>unlist</code></td> <td> <p><code>logical</code> Should the result be unlisted?</p> </td></tr> </table> <h3>Details</h3> <p><code>list.search</code> evaluates an expression (<code>expr</code>) recursively along a list (<code>.data</code>). </p> <p>If the expression results in a single-valued logical vector and its value is <code>TRUE</code>, the whole vector will be collected If it results in multi-valued or non-logical vector, the non-<code>NA</code> values resulted from the expression will be collected. </p> <p>To search whole vectors that meet certain condition, specify the expression that returns a single logical value. </p> <p>To search the specific values within the vectors, use subsetting in the expression, that is, <code>.[cond]</code> or lambda expression like <code>x -> x[cond]</code> where <code>cond</code> is a logical vector used to select the elements in the vector. </p> <h3>Examples</h3> <pre> # 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) </pre> <hr /><div style="text-align: center;">[Package <em>rlist</em> version 0.4.6.2 <a href="00Index.html">Index</a>]</div> </body></html>