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

update_data(object, revision, key, ...)

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

# S4 method for lib_df,lib_df,missing
update_data(object, revision, key, ...)

update_data(object, key, ...) <- value

# S4 method for data.frame,character,data.frame
update_data(object, key, ...) <- value

# S4 method for lib_df,missing,lib_df
update_data(object, key, ...) <- 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.

...

Further arguments passed among methods.

delete, add, update

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

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

# Adding an ID to data set iris
iris2 <- iris
iris2$id <- 1:nrow(iris2)

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

# 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_data(iris2, 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
iris2 <- update_data(iris2, iris_mod,
  key = "id", delete = TRUE, add = TRUE,
  update = TRUE
)