Title: | A Toolbox for Non-Tabular Data Manipulation |
---|---|
Description: | Provides a set of functions for data manipulation with list objects, including mapping, filtering, grouping, sorting, updating, searching, and other useful functions. Most functions are designed to be pipeline friendly so that data processing with lists can be chained. |
Authors: | Kun Ren <[email protected]> |
Maintainer: | Kun Ren <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.4.6.2 |
Built: | 2025-01-14 05:38:04 UTC |
Source: | https://github.com/renkun-ken/rlist |
rlist is a set of tools for working with list objects. Its goal is to make it easier to work with lists by providing a wide range of functions that operate on non-tabular data stored in them.
The package provides a set of functions for data manipulation with list objects, including mapping, filtering, grouping, sorting, updating, searching, and other useful functions. Most functions are designed to be pipeline friendly so that data processing with lists can be chained.
rlist Tutorial (https://renkun-ken.github.io/rlist-tutorial/) is a complete guide to rlist.
Convert an object to evaluating environment for list elements Users should not directly use this function
.evalwith(x)
.evalwith(x)
x |
the object |
create an environment for args
args_env(..., parent = parent.frame())
args_env(..., parent = parent.frame())
... |
objects |
parent |
parent environment |
create a list for args
args_list(...)
args_list(...)
... |
objects |
Evaluate a function with a modified default values
callwith(fun, args, dots = list(), keep.null = FALSE, envir = parent.frame())
callwith(fun, args, dots = list(), keep.null = FALSE, envir = parent.frame())
fun |
either a function or a non-empty character string naming the function to be called |
args |
a list of values to modify the default arguments of the function |
dots |
the user-specific input (usually from ...) |
keep.null |
|
envir |
the environment to evaluate the function call |
Test if a vector contains certain values
contains(table, x)
contains(table, x)
table |
the values to be matched against |
x |
the values to be matched |
Substitute ...
dots(...)
dots(...)
... |
parameters to substitute |
Get the names of an object
getnames(x, def = NULL)
getnames(x, def = NULL)
x |
the object to extract names |
def |
the value to return if the object has |
This function is used in vectorization when the names of an object
is to be supplied. NULL
value will break the vectorization while
setting def = character(1L)
makes the names vectorizable.
Check if an object is empty (has length 0)
is.empty(x)
is.empty(x)
x |
the object |
A NULL
value, zero-length vector or list have length zero,
which is called empty.
List environment
that wraps given data
and
most list functions are defined for chainable operations.Create a List environment
that wraps given data
and
most list functions are defined for chainable operations.
List(data = list())
List(data = list())
data |
A |
Most list functions are defined in List environment
.
In addition to these functions, call(fun,...)
calls
external function fun
with additional parameters specifies in
...
.
To extract the data from List x
, call x$data
or simply
x[]
.
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) m <- List(x) m$filter(type=='B')$ map(score$c1) [] m$group(type)$ map(g ~ List(g)$ map(score)$ call(unlist)$ call(mean) []) [] # Subsetting, extracting, and assigning p <- List(list(a=1,b=2)) p['a'] p[['a']] p$a <- 2 p['b'] <- NULL p[['a']] <- 3
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) m <- List(x) m$filter(type=='B')$ map(score$c1) [] m$group(type)$ map(g ~ List(g)$ map(score)$ call(unlist)$ call(mean) []) [] # Subsetting, extracting, and assigning p <- List(list(a=1,b=2)) p['a'] p[['a']] p$a <- 2 p['b'] <- NULL p[['a']] <- 3
Examine if a condition is true for all elements of a list
list.all(.data, cond, na.rm = FALSE)
list.all(.data, cond, na.rm = FALSE)
.data |
A |
cond |
A logical lambda expression |
na.rm |
logical. If true |
TRUE
if cond
is evaluated to be TRUE
for all elements in .data
.
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.all(x, type=='B') list.all(x, mean(unlist(score))>=6) list.all(x, score$c2 > 8 || score$c3 > 5, na.rm = TRUE) list.all(x, score$c2 > 8 || score$c3 > 5, na.rm = FALSE)
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.all(x, type=='B') list.all(x, mean(unlist(score))>=6) list.all(x, score$c2 > 8 || score$c3 > 5, na.rm = TRUE) list.all(x, score$c2 > 8 || score$c3 > 5, na.rm = FALSE)
Examine if a condition is true for at least one list element
list.any(.data, cond, na.rm = FALSE)
list.any(.data, cond, na.rm = FALSE)
.data |
A |
cond |
A logical lambda expression |
na.rm |
logical. If true |
TRUE
if cond
is evaluated to be TRUE
for any element in .data
.
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.any(x,type=='B') list.any(x,mean(unlist(score))>=6) list.any(x, score$c2 > 8 || score$c3 > 5, na.rm = TRUE) list.any(x, score$c2 > 8 || score$c3 > 5, na.rm = FALSE)
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.any(x,type=='B') list.any(x,mean(unlist(score))>=6) list.any(x, score$c2 > 8 || score$c3 > 5, na.rm = TRUE) list.any(x, score$c2 > 8 || score$c3 > 5, na.rm = FALSE)
Append elements to a list
list.append(.data, ...)
list.append(.data, ...)
.data |
A |
... |
A |
## Not run: x <- list(a=1,b=2,c=3) list.append(x,d=4,e=5) list.append(x,d=4,f=c(2,3)) ## End(Not run)
## Not run: x <- list(a=1,b=2,c=3) list.append(x,d=4,e=5) list.append(x,d=4,f=c(2,3)) ## End(Not run)
lapply
)Apply a function to each list element (lapply
)
list.apply(.data, .fun, ...)
list.apply(.data, .fun, ...)
.data |
A |
.fun |
|
... |
Additional parameters passed to |
Get all unique cases of a list field by expression
list.cases(.data, expr, simplify = TRUE, sorted = TRUE)
list.cases(.data, expr, simplify = TRUE, sorted = TRUE)
.data |
A |
expr |
A lambda expression. The function will returns all cases
of the elements if |
simplify |
|
sorted |
|
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.cases(x,type) list.cases(x,mean(unlist(score))) foo <- list(x = LETTERS[1:3], y = LETTERS[3:5]) list.cases(foo)
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.cases(x,type) list.cases(x,mean(unlist(score))) foo <- list(x = LETTERS[1:3], y = LETTERS[3:5]) list.cases(foo)
The function binds all list elements by column. Each element of the list is expected
to be an atomic vector, data.frame
, or data.table
of the same length.
If list elements are also lists, the binding will flatten the lists and may produce
undesired results.
list.cbind(.data)
list.cbind(.data)
.data |
|
x <- list(data.frame(i=1:5,x=rnorm(5)), data.frame(y=rnorm(5),z=rnorm(5))) list.cbind(x)
x <- list(data.frame(i=1:5,x=rnorm(5)), data.frame(y=rnorm(5),z=rnorm(5))) list.cbind(x)
In non-tabular data, a certain field may take multiple values in a collection non-exclusively. To classify these elements into different cases, this function detects all possible cases and for each case all elements are examined whether to belong to that case.
list.class(.data, ..., sorted = TRUE)
list.class(.data, ..., sorted = TRUE)
.data |
A |
... |
keys |
sorted |
|
a list of possible cases each of which contains elements belonging to the case non-exclusively.
x <- list( p1=list(name='Ken',age=24, interest=c('reading','music','movies'), lang=list(r=2,csharp=4,python=3)), p2=list(name='James',age=25, interest=c('sports','music'), lang=list(r=3,java=2,cpp=5)), p3=list(name='Penny',age=24, interest=c('movies','reading'), lang=list(r=1,cpp=4,python=2))) list.class(x,interest) list.class(x,names(lang))
x <- list( p1=list(name='Ken',age=24, interest=c('reading','music','movies'), lang=list(r=2,csharp=4,python=3)), p2=list(name='James',age=25, interest=c('sports','music'), lang=list(r=3,java=2,cpp=5)), p3=list(name='Penny',age=24, interest=c('movies','reading'), lang=list(r=1,cpp=4,python=2))) list.class(x,interest) list.class(x,names(lang))
This function removes all elements evaluated to be
TRUE
by an indicator function. The removal can be recursive
so that the resulted list surely does not include such elements in
any level.
list.clean(.data, fun = is.null, recursive = FALSE)
list.clean(.data, fun = is.null, recursive = FALSE)
.data |
A |
fun |
A |
recursive |
|
Raw data is usually not completely ready for analysis, and needs to
be cleaned up to certain standards. For example, some data operations
require that the input does not include NULL
values in any
level, therefore fun = "is.null"
and recursive = TRUE
can be useful to clean out all NULL
values in a list at any
level.
Sometimes, not only NULL
values are undesired,
empty vectors or lists are also unwanted. In this case,
fun = function(x) length(x) == 0L
can be useful to remove
all empty elements of zero length. This works because
length(NULL) == 0L
, length(list()) == 0L
and
length(numeric()) == 0L
are all TRUE
.
x <- list(a=NULL,b=list(x=NULL,y=character()),d=1,e=2) list.clean(x) list.clean(x, recursive = TRUE) list.clean(x, function(x) length(x) == 0L, TRUE)
x <- list(a=NULL,b=list(x=NULL,y=character()),d=1,e=2) list.clean(x) list.clean(x, recursive = TRUE) list.clean(x, function(x) length(x) == 0L, TRUE)
Get all common cases by expression for a list
list.common(.data, expr)
list.common(.data, expr)
.data |
|
expr |
An anonymous (or "lambda") expression to determine common cases. If one
is not specified, |
x <- list(c('a','b','c'),c('a','b'),c('b','c')) list.common(x, .) x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.common(x,type) list.common(x,names(score)) foo <- list(x = LETTERS[1:3], y = LETTERS[3:5]) list.common(foo)
x <- list(c('a','b','c'),c('a','b'),c('b','c')) list.common(x, .) x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.common(x,type) list.common(x,names(score)) foo <- list(x = LETTERS[1:3], y = LETTERS[3:5]) list.common(foo)
Count the number of elements that satisfy given condition
list.count(.data, cond)
list.count(.data, cond)
.data |
A |
cond |
A logical lambda expression for each element of |
An integer that indicates the number of elements with which cond
is evaluated
to be TRUE
.
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.count(x, type=='B') list.count(x, min(unlist(score)) >= 9)
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.count(x, type=='B') list.count(x, min(unlist(score)) >= 9)
Call a function with a list of arguments
list.do(.data, fun, ...)
list.do(.data, fun, ...)
.data |
|
fun |
The |
... |
The additional parameters passed to |
x <- lapply(1:3, function(i) { c(a=i,b=i^2)}) df <- lapply(1:3, function(i) { data.frame(a=i,b=i^2,c=letters[i])}) list.do(x, rbind)
x <- lapply(1:3, function(i) { c(a=i,b=i^2)}) df <- lapply(1:3, function(i) { data.frame(a=i,b=i^2,c=letters[i])}) list.do(x, rbind)
Exclude members of a list that meet given condition.
list.exclude(.data, cond)
list.exclude(.data, cond)
.data |
A |
cond |
A logical lambda expression to exclude items |
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.exclude(x, type=='B') list.exclude(x, min(score$c1,score$c2) >= 8)
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.exclude(x, type=='B') list.exclude(x, min(score$c1,score$c2) >= 8)
Create a list from all combinations of the supplied vectors or lists, extending the functionality of expand.grid from data frame to list.
list.expand(...)
list.expand(...)
... |
vectors or lists |
A list of all combinations of the supplied vectors or lists.
list.expand(x=1:10, y=c("a","b","c")) list.expand(x=list(c(1,2), c(2,3)), y = c("a","b","c")) list.expand( a=list(list(x=1,y="a"), list(x=2, y="b")), b=list(c("x","y"), c("y","z","w")))
list.expand(x=1:10, y=c("a","b","c")) list.expand(x=list(c(1,2), c(2,3)), y = c("a","b","c")) list.expand( a=list(list(x=1,y="a"), list(x=2, y="b")), b=list(c("x","y"), c("y","z","w")))
Extract an element from a list or vector
list.extract()
list.extract()
x <- list(a=1, b=2, c=3) list.extract(x, 1) list.extract(x, 'a')
x <- list(a=1, b=2, c=3) list.extract(x, 1) list.extract(x, 'a')
The function recursively filters the data by a given series of
conditions. The filter can be a single condition or multiple
conditions. .data
will be filtered by the first condition;
then the results will be filtered by the second condition, if any;
then the results will be filtered by the third, if any, etc. The
results only contain elements satisfying all conditions specified
in ...
.
list.filter(.data, ...)
list.filter(.data, ...)
.data |
A |
... |
logical conditions |
elements in .data
satisfying all conditions
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.filter(x, type=='B') list.filter(x, min(score$c1, score$c2) >= 8) list.filter(x, type=='B', score$c2 >= 8)
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.filter(x, type=='B') list.filter(x, min(score$c1, score$c2) >= 8) list.filter(x, type=='B', score$c2 >= 8)
Find a specific number of elements in a list or vector satisfying a given condition
list.find(.data, cond, n = 1L)
list.find(.data, cond, n = 1L)
.data |
A |
cond |
A logical lambda expression |
n |
The number of items to find. ( |
A list or vector of at most n
elements in .data
found to satisfy cond
.
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.find(x, type=='B', 1) list.find(x, min(score$c1,score$c2) >= 9)
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.find(x, type=='B', 1) list.find(x, min(score$c1,score$c2) >= 9)
Find the indices of a number of elements in a list or vector satisfying a given condition
list.findi(.data, cond, n = 1L)
list.findi(.data, cond, n = 1L)
.data |
A |
cond |
A logical lambda expression |
n |
The number of items to find. ( |
an integer vector consisting of the elements indices
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.findi(x, type=='B') list.findi(x, min(score$c1,score$c2) >= 8) list.findi(x, min(score$c1,score$c2) <= 8, n = 2)
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.findi(x, type=='B') list.findi(x, min(score$c1,score$c2) >= 8) list.findi(x, min(score$c1,score$c2) <= 8, n = 2)
Find the first element that meets a condition
list.first(.data, cond)
list.first(.data, cond)
.data |
A |
cond |
a logical lambda expression |
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.first(x, score$c1 < 10) list.first(x, score$c1 < 9 || score$c3 >= 5) # NULL for all results are NA or FALSE
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.first(x, score$c1 < 10) list.first(x, score$c1 < 9 || score$c3 >= 5) # NULL for all results are NA or FALSE
Flatten a nested list to a one-level list
list.flatten(x, use.names = TRUE, classes = "ANY")
list.flatten(x, use.names = TRUE, classes = "ANY")
x |
|
use.names |
|
classes |
A character vector of class names, or "ANY" to match any class. |
The function is essentially a slightly modified version of flatten2
provided by Tommy at stackoverflow.com who
has full credit of the implementation of this function.
p <- list(a=1,b=list(b1=2,b2=3),c=list(c1=list(c11='a',c12='x'),c2=3)) list.flatten(p) p <- list(a=1,b=list(x="a",y="b",z=10)) list.flatten(p, classes = "numeric") list.flatten(p, classes = "character")
p <- list(a=1,b=list(b1=2,b2=3),c=list(c1=list(c11='a',c12='x'),c2=3)) list.flatten(p) p <- list(a=1,b=list(x="a",y="b",z=10)) list.flatten(p, classes = "numeric") list.flatten(p, classes = "character")
Divide list/vector elements into exclusive groups
list.group(.data, ..., sorted = TRUE)
list.group(.data, ..., sorted = TRUE)
.data |
A |
... |
One or more expressions in the scope of each element to evaluate as keys |
sorted |
|
A list of group elements each contain all the elements in .data
belonging to the group
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.group(x, type) list.group(x, mean(unlist(score)))
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.group(x, type) list.group(x, mean(unlist(score)))
Insert a series of lists at the given index
list.insert(.data, index, ...)
list.insert(.data, index, ...)
.data |
A |
index |
The index at which the lists are inserted |
... |
A group of lists |
## Not run: x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.insert(x, 2, p2.1 = list(type='B',score=list(c1=8,c2=9))) ## End(Not run)
## Not run: x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.insert(x, 2, p2.1 = list(type='B',score=list(c1=8,c2=9))) ## End(Not run)
Return a logical vector that indicates if each member of a list satisfies a given condition
list.is(.data, cond, use.names = TRUE) list.if(.data, cond, use.names = TRUE)
list.is(.data, cond, use.names = TRUE) list.if(.data, cond, use.names = TRUE)
.data |
|
cond |
A logical lambda expression |
use.names |
|
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.is(x,type=='B') list.is(x,min(score$c1,score$c2) >= 8)
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.is(x,type=='B') list.is(x,min(score$c1,score$c2) >= 8)
Iterate a list by evaluating an expression on each list element
list.iter(.data, expr)
list.iter(.data, expr)
.data |
|
expr |
A lambda expression |
invisible(.data)
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.iter(x,cat(paste(type,'\n'))) list.iter(x,cat(str(.)))
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.iter(x,cat(paste(type,'\n'))) list.iter(x,cat(str(.)))
Join two lists by single or multiple keys
list.join(x, y, xkey, ykey, ..., keep.order = TRUE)
list.join(x, y, xkey, ykey, ..., keep.order = TRUE)
x |
The first list |
y |
The second list |
xkey |
A lambda expression that determines the key for list |
ykey |
A lambda expression that determines the key for list |
... |
The additional parameters passed to |
keep.order |
Should the order of |
l1 <- list(p1=list(name='Ken',age=20), p2=list(name='James',age=21), p3=list(name='Jenny',age=20)) l2 <- list(p1=list(name='Jenny',age=20,type='A'), p2=list(name='Ken',age=20,type='B'), p3=list(name='James',age=22,type='A')) list.join(l1, l2, name) list.join(l1, l2, .[c('name','age')])
l1 <- list(p1=list(name='Ken',age=20), p2=list(name='James',age=21), p3=list(name='Jenny',age=20)) l2 <- list(p1=list(name='Jenny',age=20,type='A'), p2=list(name='Ken',age=20,type='B'), p3=list(name='James',age=22,type='A')) list.join(l1, l2, name) list.join(l1, l2, .[c('name','age')])
Find the last element that meets a condition
list.last(.data, cond)
list.last(.data, cond)
.data |
A |
cond |
a logical lambda expression |
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.last(x, score$c1 < 10) list.last(x, score$c1 < 9 || score$c3 >= 5) # NULL for all results are NA or FALSE
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.last(x, score$c1 < 10) list.last(x, score$c1 < 9 || score$c3 >= 5) # NULL for all results are NA or FALSE
Load a list from file
list.load( file, type = tools::file_ext(file), ..., guess = c("json", "yaml", "rds", "rdata", "xml"), action = c("none", "merge", "ungroup"), progress = length(file) >= 5L )
list.load( file, type = tools::file_ext(file), ..., guess = c("json", "yaml", "rds", "rdata", "xml"), action = c("none", "merge", "ungroup"), progress = length(file) >= 5L )
file |
a |
type |
The type of input which, by default, is determined by file extension. Currently supports RData, RDS, JSON, YAML. |
... |
Additional parameters passed to the loader function |
guess |
a |
action |
The post-processing action if multiple files are supplied. This parameter will be ignored if only a single file is supplied.
|
progress |
|
## Not run: list.load('list.rds') list.load('list.rdata') list.load('list.yaml') list.load('list.json') ## End(Not run)
## Not run: list.load('list.rds') list.load('list.rdata') list.load('list.yaml') list.load('list.json') ## End(Not run)
Map each element in a list or vector by an expression.
list.map(.data, expr)
list.map(.data, expr)
.data |
a |
expr |
A lambda expression |
A list
in which each element is mapped by expr
in .data
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.map(x, type) list.map(x, min(score$c1,score$c2))
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.map(x, type) list.map(x, min(score$c1,score$c2))
Map multiple lists with an expression
list.maps(expr, ...)
list.maps(expr, ...)
expr |
An implicit lambda expression where only |
... |
Named arguments of lists with equal length. The names of the lists are available as symbols that represent the element for each list. |
## Not run: l1 <- list(p1=list(x=1,y=2), p2=list(x=3,y=4), p3=list(x=1,y=3)) l2 <- list(2,3,5) list.maps(a$x*b+a$y,a=l1,b=l2) list.maps(..1$x*..2+..1$y,l1,l2) ## End(Not run)
## Not run: l1 <- list(p1=list(x=1,y=2), p2=list(x=3,y=4), p3=list(x=1,y=3)) l2 <- list(2,3,5) list.maps(a$x*b+a$y,a=l1,b=l2) list.maps(..1$x*..2+..1$y,l1,l2) ## End(Not run)
Map each member of a list by an expression to a vector.
list.mapv(.data, expr, as, use.names = TRUE)
list.mapv(.data, expr, as, use.names = TRUE)
.data |
a |
expr |
a lambda expression |
as |
the mode to corece. Missing to |
use.names |
Should the names of the results be preserved? |
A vector
in which each element is mapped by expr
in .data
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.mapv(x, type) list.mapv(x, min(score$c1,score$c2))
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.mapv(x, type) list.mapv(x, min(score$c1,score$c2))
Select members of a list that match given regex pattern
list.match(.data, pattern, ...)
list.match(.data, pattern, ...)
.data |
A |
pattern |
|
... |
Additional parameters to pass to |
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.match(x,'p[12]') list.match(x,'3')
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.match(x,'p[12]') list.match(x,'3')
The function merges a number of lists in sequential order
by modifyList
, that is, the later list always
modifies the former list and form a merged list, and the
resulted list is again being merged with the next list.
The process is repeated until all lists in ...
or
list
are exausted.
list.merge(...)
list.merge(...)
... |
named lists |
List merging is usually useful in the merging of program settings or configuraion with multiple versions across time, or multiple administrative levels. For example, a program settings may have an initial version in which most keys are defined and specified. In later versions, partial modifications are recorded. In this case, list merging can be useful to merge all versions of settings in release order of these versions. The result is an fully updated settings with all later modifications applied.
l1 <- list(a=1,b=list(x=1,y=1)) l2 <- list(a=2,b=list(z=2)) l3 <- list(a=2,b=list(x=3)) list.merge(l1,l2,l3)
l1 <- list(a=1,b=list(x=1,y=1)) l2 <- list(a=2,b=list(z=2)) l3 <- list(a=2,b=list(x=3)) list.merge(l1,l2,l3)
Get or set the names of a list by expression
list.names(.data, expr)
list.names(.data, expr)
.data |
A |
expr |
the expression whose value will be set as the name
for each list element. If missing then the names of the list will be
returned. If |
list.names(c(1,2,3)) list.names(c(a=1,b=2,c=3)) list.names(c(1,2,3),letters[.]) list.names(list(list(name='A',value=10),list(name='B',value=20)), name)
list.names(c(1,2,3)) list.names(c(a=1,b=2,c=3)) list.names(c(1,2,3),letters[.]) list.names(list(list(name='A',value=10),list(name='B',value=20)), name)
Give the order of each list element by expression
list.order(.data, ..., keep.names = FALSE, na.last = TRUE)
list.order(.data, ..., keep.names = FALSE, na.last = TRUE)
.data |
A |
... |
A group of lambda expressions |
keep.names |
Whether to keep the names of |
na.last |
The way to deal with |
an integer
vector.
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.order(x, type, (score$c2)) # order by type (ascending) and score$c2 (descending) list.order(x, min(score$c1,score$c2)) list.order(x, min(score$c1,score$c2), keep.names=TRUE)
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.order(x, type, (score$c2)) # order by type (ascending) and score$c2 (descending) list.order(x, min(score$c1,score$c2)) list.order(x, min(score$c1,score$c2), keep.names=TRUE)
This function converts an object representing data to
list that represents the same data. For example, a
data.frame
stored tabular data column-wisely,
that is, each column represents a vector of a certain
type. list.parse
converts a data.frame
to
a list which represents the data row-wisely so that it
can be more convinient to perform other non-tabular data
manipulation methods.
list.parse(x, ...) ## Default S3 method: list.parse(x, ...) ## S3 method for class 'matrix' list.parse(x, ...) ## S3 method for class 'data.frame' list.parse(x, ...) ## S3 method for class 'character' list.parse(x, type, ...)
list.parse(x, ...) ## Default S3 method: list.parse(x, ...) ## S3 method for class 'matrix' list.parse(x, ...) ## S3 method for class 'data.frame' list.parse(x, ...) ## S3 method for class 'character' list.parse(x, type, ...)
x |
|
... |
Additional parameters passed to converter function |
type |
The type of data to parse. Currently json and yaml are supported. |
list
object representing the data in x
x <- data.frame(a=1:3,type=c('A','C','B')) list.parse(x) x <- matrix(rnorm(1000),ncol=5) rownames(x) <- paste0('item',1:nrow(x)) colnames(x) <- c('a','b','c','d','e') list.parse(x) z <- ' a: type: x class: A registered: yes ' list.parse(z, type='yaml')
x <- data.frame(a=1:3,type=c('A','C','B')) list.parse(x) x <- matrix(rnorm(1000),ncol=5) rownames(x) <- paste0('item',1:nrow(x)) colnames(x) <- c('a','b','c','d','e') list.parse(x) z <- ' a: type: x class: A registered: yes ' list.parse(z, type='yaml')
Prepend elements to a list
list.prepend(.data, ...)
list.prepend(.data, ...)
.data |
A |
... |
The |
x <- list(a=1,b=2,c=3) list.prepend(x, d=4, e=5) list.prepend(x, d=4, f=c(2,3))
x <- list(a=1,b=2,c=3) list.prepend(x, d=4, e=5) list.prepend(x, d=4, f=c(2,3))
The function binds all list elements by row. Each element of the list is expected
to be an atomic vector, data.frame
, or data.table
. If list elements
are also lists, the result can be a list-valued matrix. In this case,
list.stack
may produce a better result.
list.rbind(.data)
list.rbind(.data)
.data |
|
x <- lapply(1:3,function(i) { c(a=i,b=i^2)}) df <- lapply(1:3,function(i) { data.frame(a=i,b=i^2,c=letters[i])}) list.rbind(x) list.rbind(df)
x <- lapply(1:3,function(i) { c(a=i,b=i^2)}) df <- lapply(1:3,function(i) { data.frame(a=i,b=i^2,c=letters[i])}) list.rbind(x) list.rbind(df)
Remove members from a list by index or name
list.remove(.data, range = integer())
list.remove(.data, range = integer())
.data |
A |
range |
A numeric vector of indices or
a character vector of names to remove from |
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.remove(x, 'p1') list.remove(x, c(1,2))
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.remove(x, 'p1') list.remove(x, c(1,2))
Reverse a list
list.reverse(.data)
list.reverse(.data)
.data |
A |
x <- list(a=1,b=2,c=3) list.reverse(x)
x <- list(a=1,b=2,c=3) list.reverse(x)
Sample a list or vector
list.sample(.data, size, replace = FALSE, weight = 1, prob = NULL)
list.sample(.data, size, replace = FALSE, weight = 1, prob = NULL)
.data |
A |
size |
|
replace |
|
weight |
A lambda expression to determine the weight of
each list member, which only takes effect if |
prob |
A |
x <- list(a = 1, b = c(1,2,3), c = c(2,3,4)) list.sample(x, 2, weight = sum(.))
x <- list(a = 1, b = c(1,2,3), c = c(2,3,4)) list.sample(x, 2, weight = sum(.))
Save a list to a file
list.save(x, file, type = tools::file_ext(file), ...)
list.save(x, file, type = tools::file_ext(file), ...)
x |
The list to save |
file |
The file for output |
type |
The type of output which, by default, is determined by file extension. Currently supports RData, RDS, JSON, YAML. |
... |
Additional parameters passed to the output function |
x
will be returned.
## Not run: x <- lapply(1:5,function(i) data.frame(a=i,b=i^2)) list.save(x, 'list.rds') list.save(x, 'list.rdata') list.save(x, 'list.yaml') list.save(x, 'list.json') ## End(Not run)
## Not run: x <- lapply(1:5,function(i) data.frame(a=i,b=i^2)) list.save(x, 'list.rds') list.save(x, 'list.rdata') list.save(x, 'list.yaml') list.save(x, 'list.json') ## End(Not run)
Search a list recusively by an expression
list.search(.data, expr, classes = "ANY", n, unlist = FALSE)
list.search(.data, expr, classes = "ANY", n, unlist = FALSE)
.data |
A |
expr |
a lambda expression |
classes |
a character vector of class names that restrict the search.
By default, the range is unrestricted ( |
n |
the maximal number of vectors to return |
unlist |
|
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.
# 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)
# 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)
Select by name or expression for each member of a list
list.select(.data, ...)
list.select(.data, ...)
.data |
A |
... |
A group of implicit labmda expressions |
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.select(x, type) list.select(x, tp = type) list.select(x, type, score) list.select(x, type, score.range = range(unlist(score)))
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.select(x, type) list.select(x, tp = type) list.select(x, type, score) list.select(x, type, score.range = range(unlist(score)))
Serialize a list
list.serialize(x, file, type = tools::file_ext(file), ...)
list.serialize(x, file, type = tools::file_ext(file), ...)
x |
|
file |
The file for output |
type |
The type of serialization, including native serializer and json serializer, which is by default determined by file extension |
... |
Additional parameters passed to the serializer function |
## Not run: x <- list(a=1,b=2,c=3) list.serialize(x,'test.dat') list.serialize(x,'test.json') ## End(Not run)
## Not run: x <- list(a=1,b=2,c=3) list.serialize(x,'test.dat') list.serialize(x,'test.json') ## End(Not run)
Skip the first n
elements of a list or vector and
return the remaining elements if any.
list.skip(.data, n)
list.skip(.data, n)
.data |
A |
n |
|
list.skipWhile
, list.take
,
list.takeWhile
x <- list(a=1,b=2,c=3) list.skip(x, 1) list.skip(x, 2)
x <- list(a=1,b=2,c=3) list.skip(x, 1) list.skip(x, 2)
Keep skipping elements in a list or vector while a condition holds for the element. As long as the condition is violated, the element will be kept and all remaining elements are returned.
list.skipWhile(.data, cond)
list.skipWhile(.data, cond)
.data |
A |
cond |
A logical lambda expression |
list.skip
, list.take
,
list.takeWhile
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.skipWhile(x, type=='A') list.skipWhile(x, min(score$c1,score$c2) >= 8)
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.skipWhile(x, type=='A') list.skipWhile(x, min(score$c1,score$c2) >= 8)
Sort a list by given expressions
list.sort(.data, ..., na.last = NA)
list.sort(.data, ..., na.last = NA)
.data |
a |
... |
A group of lambda expressions. For each expression, the data is sorted ascending by default unless the expression is enclosed by (). |
na.last |
The way to deal with |
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.sort(x, type, (score$c2)) # sort by score$c2 in descending order list.sort(x, min(score$c1,score$c2))
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.sort(x, type, (score$c2)) # sort by score$c2 in descending order list.sort(x, min(score$c1,score$c2))
Stack all list elements to tabular data
list.stack(.data, ..., data.table = FALSE)
list.stack(.data, ..., data.table = FALSE)
.data |
|
... |
additional parameters passed to |
data.table |
|
## Not run: x <- lapply(1:3, function(i) { list(a=i,b=i^2) }) list.stack(x) x <- lapply(1:3, function(i) { list(a=i,b=i^2,c=letters[i])}) list.stack(x) x <- lapply(1:3, function(i) { data.frame(a=i,b=i^2,c=letters[i]) }) list.stack(x) x <- lapply(1:3, function(i) { data.frame(a=c(i,i+1), b=c(i^2,i^2+1))}) list.stack(x) ## End(Not run)
## Not run: x <- lapply(1:3, function(i) { list(a=i,b=i^2) }) list.stack(x) x <- lapply(1:3, function(i) { list(a=i,b=i^2,c=letters[i])}) list.stack(x) x <- lapply(1:3, function(i) { data.frame(a=i,b=i^2,c=letters[i]) }) list.stack(x) x <- lapply(1:3, function(i) { data.frame(a=c(i,i+1), b=c(i^2,i^2+1))}) list.stack(x) ## End(Not run)
Subset a list
list.subset()
list.subset()
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.subset(x, c('p1','p2')) list.subset(x, grepl('^p', names(x))) ## Not run: list.subset(x, stringdist::stringdist(names(x), 'x1') <= 1) ## End(Not run)
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.subset(x, c('p1','p2')) list.subset(x, grepl('^p', names(x))) ## Not run: list.subset(x, stringdist::stringdist(names(x), 'x1') <= 1) ## End(Not run)
Generate a table for a list by expression
list.table(.data, ..., table.args = list(useNA = "ifany"))
list.table(.data, ..., table.args = list(useNA = "ifany"))
.data |
A |
... |
A group of lambda expressions. If missing,
|
table.args |
|
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.table(x, type) list.table(x, type, c1 = score$c1) list.table(x, type, score$c1, table.args = list(dnn=c('type','c1')))
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.table(x, type) list.table(x, type, c1 = score$c1) list.table(x, type, score$c1, table.args = list(dnn=c('type','c1')))
Take the first n
elements out from a list or
vector.
list.take(.data, n, force = FALSE)
list.take(.data, n, force = FALSE)
.data |
|
n |
|
force |
|
list.takeWhile
, list.skip
,
list.skipWhile
x <- list(a=1,b=2,c=3) list.take(x,1) list.take(x,10)
x <- list(a=1,b=2,c=3) list.take(x,1) list.take(x,10)
Keep taking elements out from a list or vector while a condition holds for the element. If the condition is violated for an element, the element will not be taken and all taken elements will be returned.
list.takeWhile(.data, cond)
list.takeWhile(.data, cond)
.data |
|
cond |
A logical lambda expression |
list.take
, list.skip
,
list.skipWhile
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.takeWhile(x, type=='B') list.takeWhile(x, min(score$c1,score$c2) >= 8)
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.takeWhile(x, type=='B') list.takeWhile(x, min(score$c1,score$c2) >= 8)
This functon reverses the grouping operation by taking out second-level elements of a nested list and removing the labels of the first-level elements. For example, a list may be created from paged data, that is, its first-level elements only indicate the page container. To unpage the list, the first-level elements must be removed and their inner elements should be taken out to to the first level.
list.ungroup(.data, level = 1L, ..., group.names = FALSE, sort.names = FALSE)
list.ungroup(.data, level = 1L, ..., group.names = FALSE, sort.names = FALSE)
.data |
|
level |
integer to indicate to which level of list elements should be ungroupped to the first level. |
... |
Preserved use of parameter passing |
group.names |
|
sort.names |
|
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) xg <- list.group(x, type) list.ungroup(xg) x <- list(a = list(a1 = list(x=list(x1=2,x2=3),y=list(y1=1,y2=3))), b = list(b1 = list(x=list(x1=2,x2=6),y=list(y1=3,y2=2)))) list.ungroup(x, level = 1) list.ungroup(x, level = 2) list.ungroup(x, level = 2, group.names = TRUE)
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) xg <- list.group(x, type) list.ungroup(xg) x <- list(a = list(a1 = list(x=list(x1=2,x2=3),y=list(y1=1,y2=3))), b = list(b1 = list(x=list(x1=2,x2=6),y=list(y1=3,y2=2)))) list.ungroup(x, level = 1) list.ungroup(x, level = 2) list.ungroup(x, level = 2, group.names = TRUE)
Unserialize a file
list.unserialize(file, type = tolower(tools::file_ext(file)), ...)
list.unserialize(file, type = tolower(tools::file_ext(file)), ...)
file |
The file as input |
type |
The type of serialization, including native unserializer and json unserializer, which is by default determined by file extension |
... |
Additional parameters passed to the unserializer function |
## Not run: list.unserialize('test.dat') list.unserialize('test.json') ## End(Not run)
## Not run: list.unserialize('test.dat') list.unserialize('test.json') ## End(Not run)
Transform a list of elements with similar structure into a list of decoupled fields
list.unzip( .data, .fields = c("intersect", "union"), ..., .aggregate = "simplify2array", .missing = NA )
list.unzip( .data, .fields = c("intersect", "union"), ..., .aggregate = "simplify2array", .missing = NA )
.data |
A |
.fields |
|
... |
The custom aggregate functions. Can be a named list of functions or
character vectors. If a function is specified as a list of functions, then the
functions will be evaluated recursively on the result of the field. Use |
.aggregate |
The default aggregate function, by default, |
.missing |
When |
list.unzip(list(p1 = list(a = 1, b = 2), p2 = list(a = 2, b = 3))) list.unzip(list(p1 = list(a = 1, b = 2), p2 = list(a = 2, b = 3, c = 4))) list.unzip(list(p1 = list(a = 1, b = 2), p2 = list(a = 2, b = 3, c = 4)), 'union') list.unzip(list(p1 = list(a = 1, b = 2), p2 = list(a = 2, b = 3, c = 4)), 'union', a = 'identity') list.unzip(list(p1 = list(a = 1, b = 2), p2 = list(a = 2, b = 3, c = 4)), 'intersect', a = NULL) x <- list(april = list(n_days = 30, holidays = list(list('2015-04-01', 'april fools'), list('2015-04-05', 'easter')), month_info = c(number = '4', season = 'spring')), july = list(n_days = 31, holidays = list(list('2014-07-04', 'july 4th')), month_info = c(number = '7', season = 'summer'))) list.unzip(x, holidays = c('list.ungroup', 'unname', 'list.stack', function(df) setNames(df, c("date", "name"))))
list.unzip(list(p1 = list(a = 1, b = 2), p2 = list(a = 2, b = 3))) list.unzip(list(p1 = list(a = 1, b = 2), p2 = list(a = 2, b = 3, c = 4))) list.unzip(list(p1 = list(a = 1, b = 2), p2 = list(a = 2, b = 3, c = 4)), 'union') list.unzip(list(p1 = list(a = 1, b = 2), p2 = list(a = 2, b = 3, c = 4)), 'union', a = 'identity') list.unzip(list(p1 = list(a = 1, b = 2), p2 = list(a = 2, b = 3, c = 4)), 'intersect', a = NULL) x <- list(april = list(n_days = 30, holidays = list(list('2015-04-01', 'april fools'), list('2015-04-05', 'easter')), month_info = c(number = '4', season = 'spring')), july = list(n_days = 31, holidays = list(list('2014-07-04', 'july 4th')), month_info = c(number = '7', season = 'summer'))) list.unzip(x, holidays = c('list.ungroup', 'unname', 'list.stack', function(df) setNames(df, c("date", "name"))))
The function updates each element of a list by evaluating
a group of expressions in the scope of the element. If the
name of an expression alreadys exists in an list element,
then the field with the name will be updated. Otherwise,
the value with the name will be appended to the list
element. The functionality is essentially done by
modifyList
.
list.update(.data, ..., keep.null = FALSE)
list.update(.data, ..., keep.null = FALSE)
.data |
|
... |
A group of lambda expressions |
keep.null |
Should |
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.update(x, high=max(score$c1,score$c2), low=min(score$c1,score$c2)) list.update(x, exams=length(score)) list.update(x, grade=ifelse(type=='A', score$c1, score$c2)) list.update(x, score=list(min=0, max=10))
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.update(x, high=max(score$c1,score$c2), low=min(score$c1,score$c2)) list.update(x, exams=length(score)) list.update(x, grade=ifelse(type=='A', score$c1, score$c2)) list.update(x, score=list(min=0, max=10))
Give the indices of list elements satisfying a given condition
list.which(.data, cond)
list.which(.data, cond)
.data |
A |
cond |
A logical lambda expression |
an integer
vector
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.which(x, type == 'B') list.which(x, min(score$c1,score$c2) >= 8)
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) list.which(x, type == 'B') list.which(x, min(score$c1,score$c2) >= 8)
Combine multiple lists element-wisely.
list.zip(..., use.argnames = TRUE, use.names = TRUE)
list.zip(..., use.argnames = TRUE, use.names = TRUE)
... |
|
use.argnames |
|
use.names |
|
x <- list(1,2,3) y <- list('x','y','z') list.zip(num=x,sym=y)
x <- list(1,2,3) y <- list('x','y','z') list.zip(num=x,sym=y)
A non-tabular data of the hourly weather conditions of the New York City from 2013-01-01 to 2013-03-01.
nyweather
nyweather
See https://openweathermap.org/weather-data
Fetch date: 2014-11-23.
Processed by rlist.
To retrieve the data, please visit https://openweathermap.org/api for API usage.
Make names for unnamed symbol arguments
set_argnames(args, data = args)
set_argnames(args, data = args)
args |
the unevaluated argument list |
data |
the list to be named ( |
The elements of an unevaluated list of arguments may or may not
have names as given by user. For example, list.select
requires user
to specify the fields to select. These fields are unevaluated arguments,
some of which are symbols and others are calls. For the symbols, it is natural
to make the resulted lists to have the same name for the particular arguments.
Subset a list by a logical condition
## S3 method for class 'list' subset(x, subset, select, ...)
## S3 method for class 'list' subset(x, subset, select, ...)
x |
The list to subset |
subset |
A logical lambda expression of subsetting condition |
select |
A lambda expression to evaluate for each selected item |
... |
Additional parameters |
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) subset(x, type == 'B') subset(x, select = score) subset(x, min(score$c1, score$c2) >= 8, data.frame(score)) subset(x, type == 'B', score$c1) do.call(rbind, subset(x, min(score$c1, score$c2) >= 8, data.frame(score)))
x <- list(p1 = list(type='A',score=list(c1=10,c2=8)), p2 = list(type='B',score=list(c1=9,c2=9)), p3 = list(type='B',score=list(c1=9,c2=7))) subset(x, type == 'B') subset(x, select = score) subset(x, min(score$c1, score$c2) >= 8, data.frame(score)) subset(x, type == 'B', score$c1) do.call(rbind, subset(x, min(score$c1, score$c2) >= 8, data.frame(score)))
Try to evaluate an expression and return a default value if an error occurs or otherwise return its value.
tryEval(expr, def = NULL)
tryEval(expr, def = NULL)
expr |
the expression to evaluate |
def |
the default value if an error occurs in the evaluation
of |
x <- list(a=c(x=1,y=2),b=c(x=2,p=3)) list.map(x, tryEval(x+y, NA))
x <- list(a=c(x=1,y=2),b=c(x=2,p=3)) list.map(x, tryEval(x+y, NA))
Try to get the value of a symbol if exists or return a default value
tryGet(symbol, def = NULL, ..., envir = parent.frame())
tryGet(symbol, def = NULL, ..., envir = parent.frame())
symbol |
the symbol to examine |
def |
the default value if the symbol does not exist |
... |
additional parameters passed to |
envir |
the environment to examine whether the symbol exists and get the symbol |
By default, the symbol is examined in envir
without inheritance,
that is, if the symbol does not exist in envir
the default value
def
will be returned.
x <- list(a=c(x=1,y=2),b=c(x=2,p=3)) list.map(x, tryGet(y,0))
x <- list(a=c(x=1,y=2),b=c(x=2,p=3)) list.map(x, tryGet(y,0))