Skip to contents

This function compares two versions of the same data frame and detect changes as additions, deleted entries or updates (modified entries).

A method to compare lib_df objects is also provided as well as a replace method.

Usage

# S3 method for data.frame
update(object, revision, key, delete = FALSE, add = FALSE, update = FALSE, ...)

# S3 method for lib_df
update(
  object,
  revision,
  key = "bibtexkey",
  delete = FALSE,
  add = FALSE,
  update = FALSE,
  ...
)

update(object, ...) <- value

# S4 method for data.frame,data.frame
update(object, key, delete = FALSE, add = FALSE, update = FALSE, ...) <- value

# S4 method for lib_df,lib_df
update(
  object,
  key = "bibtexkey",
  delete = FALSE,
  add = FALSE,
  update = FALSE,
  ...
) <- value

Arguments

object

A data frame or a lib_df object representing the original version.

revision

The updated version of 'object' to be compared.

key

A character value indicating the column used as identifier. This variable have to be in both versions otherwise this function will retrieve an error.

delete, add, update

A character value indicating whether the action should be carried out. If all are 'FALSE', this function will just report differences as done by compare_df.

...

Further arguments passed among methods.

value

The updated version of 'object' in the replace methods.

Value

Either an invisible output with a print in the console or an updated object of class lib_df.

Examples

# modifying the data set iris
data(iris)
iris$id <- 1:nrow(iris) # ID column added

# rows to add using mean values per species
iris_mod <- aggregate(cbind(
  Sepal.Length, Sepal.Width, Petal.Length,
  Petal.Width
) ~ Species, data = iris, FUN = mean)
iris_mod$id <- (1:nrow(iris_mod)) + nrow(iris)
iris_mod <- do.call(rbind, list(iris, iris_mod[, colnames(iris)]))

# delete some entries
iris_mod <- iris_mod[-c(15, 75, 105, 145), ]

# modify entries
iris_mod$Petal.Length[c(20, 30)] <- 0
iris_mod$Petal.Width[c(20, 50)] <- 0

# just a comparison
update(iris, iris_mod, key = "id")
#> ## deleted entries (4):
#> '15' '75' '105' '145'
#> 
#> ## added entries (3):
#> '151' '152' '153'
#> 
#> ## updates in entry '21'
#>  - old Petal.Length: 1.7
#>  - new Petal.Length: 0
#> 
#>  - old Petal.Width: 0.2
#>  - new Petal.Width: 0
#> 
#> ## updates in entry '31'
#>  - old Petal.Length: 1.6
#>  - new Petal.Length: 0
#> 
#> ## updates in entry '51'
#>  - old Petal.Width: 1.4
#>  - new Petal.Width: 0
#> 

# do update
iris <- update(iris, iris_mod,
  key = "id", delete = TRUE, add = TRUE,
  update = TRUE
)