I have data that call mydata. mydata contains full september data from Sales Alabama Store. Here it is:
# My Data
library(lubridate)
gen_seq <- seq(ymd_h("2021-09-01-00"), ymd_h("2021-09-30-23"), by = "hours")
datex <- date(gen_seq)
hourx <- hour(gen_seq)
store <- "Alabama"
sales <- round(runif(length(datex), 10, 50), 0)
mydata <- data.frame(datex, hourx, store, sales)
And now i have a new revision from date "2021-09-15" & "2021-09-16", It is the revision:
# New My Data
library(lubridate)
gen_seq <- seq(ymd_h("2021-09-15-00"), ymd_h("2021-09-16-23"), by = "hours")
datex <- date(gen_seq)
hourx <- hour(gen_seq)
store <- "Alabama"
sales <- round(runif(length(datex), 10, 50), 0)
newmydata <- data.frame(datex, hourx, store, sales)
How do i replace it? Thanks
CodePudding user response:
You can join the two datasets and update the values with coalesce
.
library(dplyr)
left_join(mydata, newmydata, by = c("datex", "hourx", "store")) %>%
mutate(sales = coalesce(sales.y, sales.x)) %>%
select(-sales.x, -sales.y)
In base R,
merge(mydata, newmydata, by= c("datex", "hourx", "store"), all.x = TRUE) |>
transform(sales = ifelse(is.na(sales.y), sales.x, sales.y))