Learn R Programming

gdata (version 2.19.0)

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

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 list
update(object, new, unnamed=FALSE, ...)
# S3 method for data.frame
update(object, new, by, by.x=by, by.y=by, append=TRUE, verbose=FALSE, ...)

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 dataframe 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.

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 dataframe rows.

append

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

verbose

Logical. If TRUE progress messages will be displayed.

...

optional method arguments (ignored)

Author

Gregory R. Warnes greg@warnes.net

See Also

Examples

Run this code
#----
# Examples for 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

#----
# Examples for 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)

Run the code above in your browser using DataLab