update.list: Update the elements of a list, or rows of a data frame

View source: R/update.list.R

update.listR Documentation

Update the elements of a list, or rows of a data frame

Description

For a list, update the elements of a list to contain all of the named elements of a new list, overwriting elements with the same name, and (optionally) copying unnamed elements. For a data frame, replace the rows of a data frame by corresponding rows in 'new' with the same value for 'by'.

Usage

## S3 method for class 'list'
update(object, new, unnamed=FALSE, ...)
## S3 method for class 'data.frame'
update(object, new, by, by.x=by, by.y=by,
       append=TRUE, verbose=FALSE, ...)

Arguments

object

List or data frame to be updated.

new

List or data frame containing new elements/rows.

unnamed

Logical. If TRUE, unnamed list elements of new will be appended to object.

by, by.x, by.y

Character. Name of column to use for matching data frame rows.

append

Logical. If TRUE, items in new with no match in object will be appended to the data frame.

verbose

Logical. If TRUE progress messages will be displayed.

...

optional method arguments (ignored).

Value

update.list

a list a constructed from the elements of object, with named elements of new replacing corresponding named elements from object, and non-corresponding elements of new appended. If unnamed=TRUE, unnamed elements of new will be appended.

update.data.frame

a data frame constructed from the rows of object with rows where values in by.x equal the values in by.y replaced by the corresponding row in new. If append=TRUE, any elements of new without no matching rows in object will be appended.

Note

These methods can be called directly or as via the S3 base method for update.

Author(s)

Gregory R. Warnes greg@warnes.net

See Also

update, merge

Examples

# Update list
old <- list(a=1,b="red",c=1.37)
new <- list(b="green",c=2.4)

update(old, new)
update.list(old,new)  # equivalent

older <- list(a=0, b="orange", 4, 5, 6)
newer <- list(b="purple", 7, 8, 9)
update(older, newer)               # ignores unnamed elements of newer
update(older, newer, unnamed=TRUE) # appends unnamed elements of newer

# Update data frame
old <- data.frame(letter=letters[1:5], number=1:5)
new <- data.frame(letter=letters[c(5, 1, 7)], number=c(-5, -1, -7))

update(old, new, by="letter") # default is append=TRUE
update(old, new, by="letter", append=FALSE)
update(old, new, by="letter", verbose=FALSE)

gdata documentation built on Oct. 22, 2024, 9:06 a.m.