list.update: Update a list by appending or modifying its elements.

Description Usage Arguments Examples

View source: R/list.update.R

Description

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.

Usage

1
list.update(.data, ..., keep.null = FALSE)

Arguments

.data

list

...

A group of labmda expressions

keep.null

Should NULL values be preserved for modifyList

Examples

1
2
3
4
5
6
7
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))

Example output

$p1
$p1$type
[1] "A"

$p1$score
$p1$score$c1
[1] 10

$p1$score$c2
[1] 8


$p1$high
[1] 10

$p1$low
[1] 8


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

$p2$score
$p2$score$c1
[1] 9

$p2$score$c2
[1] 9


$p2$high
[1] 9

$p2$low
[1] 9


$p3
$p3$type
[1] "B"

$p3$score
$p3$score$c1
[1] 9

$p3$score$c2
[1] 7


$p3$high
[1] 9

$p3$low
[1] 7


$p1
$p1$type
[1] "A"

$p1$score
$p1$score$c1
[1] 10

$p1$score$c2
[1] 8


$p1$exams
[1] 2


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

$p2$score
$p2$score$c1
[1] 9

$p2$score$c2
[1] 9


$p2$exams
[1] 2


$p3
$p3$type
[1] "B"

$p3$score
$p3$score$c1
[1] 9

$p3$score$c2
[1] 7


$p3$exams
[1] 2


$p1
$p1$type
[1] "A"

$p1$score
$p1$score$c1
[1] 10

$p1$score$c2
[1] 8


$p1$grade
[1] 10


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

$p2$score
$p2$score$c1
[1] 9

$p2$score$c2
[1] 9


$p2$grade
[1] 9


$p3
$p3$type
[1] "B"

$p3$score
$p3$score$c1
[1] 9

$p3$score$c2
[1] 7


$p3$grade
[1] 7


$p1
$p1$type
[1] "A"

$p1$score
$p1$score$c1
[1] 10

$p1$score$c2
[1] 8

$p1$score$min
[1] 0

$p1$score$max
[1] 10



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

$p2$score
$p2$score$c1
[1] 9

$p2$score$c2
[1] 9

$p2$score$min
[1] 0

$p2$score$max
[1] 10



$p3
$p3$type
[1] "B"

$p3$score
$p3$score$c1
[1] 9

$p3$score$c2
[1] 7

$p3$score$min
[1] 0

$p3$score$max
[1] 10

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