Learn R Programming

crank (version 1.1-2)

listCrawler: Descend a list, applying a function to each element.

Description

Descend a possibly nested list, seeking the element that has the extreme value of a function.

Usage

listCrawler(x,FUN=NULL,maxval=TRUE,
  retval=list(indx=vector("numeric",0),element=NULL,value=NA))

Arguments

x

The object that will be the first argument of FUN, or a possibly nested list of such objects.

FUN

A function that can accept x as its first argument.

maxval

Whether to look for maximal (TRUE) or minimal (FALSE) values of the function FUN.

retval

The list that is eventually returned.

Value

A list containing:

indx

the indices of the element producing the extreme value of FUN.

element

The element that produced the extremum.

value

The extreme value of FUN.

Details

listCrawler descends the list structure of x applying FUN to any non-list elements it encounters. If the value of FUN is larger or smaller than the current extremum (depending upon the value of maxval), the new value becomes the current extremum. The default value of FUN just takes the value of the elements.

See Also

list, listBuilder

Examples

Run this code
# NOT RUN {
 # a simple example using the square root function
 testlist<-list(list(9,16),list(25,list(36,49)))
 # first get the default maximum
 listCrawler(testlist,sqrt)
 # then the minimum
 listCrawler(testlist,sqrt,maxval=FALSE)
# }

Run the code above in your browser using DataLab