Say you have a dataframe:
original_values <-
tibble(
id = c("50.0", "55.0", "63.0", "65.0", "68.0"),
dest = rep("dummy", 5),
date = as_date(rep("2020-01-01", 5)),
friend = rep("Steve")
)
Imagine mine is much bigger, but this is just for illustration.
For a subset of rows, I want to update certain cells with values stored in a new dataframe.
value_corrections <-
tibble(
id = c("50.0", "63.0", "68.0"),
destination = c("Unknown", "Rush Oak Park ED", "Unknown"),
date = as_date(c("2021-06-27", "2021-07-12", "2021-08-09"))
)
Is there a succinct way to do this programmatically?
I've tried this a few different ways, but most recently:
update_discharges <- function(a_tibble, an_id, a_destination, a_date){
a_tibble[a_tibble$id == an_id, 'dest'] <- a_destination
a_tibble[a_tibble$id == an_id, 'date'] <- a_date
}
for(i in seq_along(value_corrections)) {
update_discharges(
a_tibble = original_values,
an_id = value_corrections$an_id[[i]],
a_destination = value_corrections$a_destination[[i]],
a_date = value_corrections$a_date[[i]]
)
}