So I have been trying to merge these 2 data.table
s that look like this
structure(list(orderDate = structure(c(18414, 18444, 18475, 18506,
18536, 18567, 18597, 18628, 18659, 18687, 18718, 18748, 18779
), class = "Date"), productName = c("A. De La Sota Lady", "A. De La Sota Lady",
"A. De La Sota Lady", "A. De La Sota Lady", "A. De La Sota Lady",
"A. De La Sota Lady", "A. De La Sota Lady", "A. De La Sota Lady",
"A. De La Sota Lady", "A. De La Sota Lady", "A. De La Sota Lady",
"A. De La Sota Lady", "A. De La Sota Lady"), totalOrders = c(2L,
15L, 52L, 225L, 27L, 10L, 5L, 19L, 36L, 41L, 58L, 16L, 2L)), row.names = c(NA,
-13L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x0000024e1b7d1ef0>, sorted = "orderDate")
and
structure(list(returnDate = structure(c(18444, 18475, 18506,
18536, 18567, 18597, 18628, 18659, 18687, 18718, 18748, 18779
), class = "Date"), productName = c("A. De La Sota Lady", "A. De La Sota Lady",
"A. De La Sota Lady", "A. De La Sota Lady", "A. De La Sota Lady",
"A. De La Sota Lady", "A. De La Sota Lady", "A. De La Sota Lady",
"A. De La Sota Lady", "A. De La Sota Lady", "A. De La Sota Lady",
"A. De La Sota Lady"), totalReturns = c(5L, 10L, 129L, 73L, 18L,
3L, 8L, 15L, 43L, 44L, 30L, 6L), orderDate = structure(c(18444,
18475, 18506, 18536, 18567, 18597, 18628, 18659, 18687, 18718,
18748, 18779), class = "Date")), row.names = c(NA, -12L), class = c("data.table",
"data.frame"), .internal.selfref = <pointer: 0x0000024e1b7d1ef0>, sorted = "orderDate")
the result is a merged data.table
structure(list(orderDate = structure(c(18444, 18475, 18506, 18536,
18567, 18597, 18628, 18659, 18687, 18718, 18748, 18779), class = "Date"),
productName = c("A. De La Sota Lady", "A. De La Sota Lady",
"A. De La Sota Lady", "A. De La Sota Lady", "A. De La Sota Lady",
"A. De La Sota Lady", "A. De La Sota Lady", "A. De La Sota Lady",
"A. De La Sota Lady", "A. De La Sota Lady", "A. De La Sota Lady",
"A. De La Sota Lady"), totalOrders = c(15L, 52L, 225L, 27L,
10L, 5L, 19L, 36L, 41L, 58L, 16L, 2L), totalReturns = c(5L,
10L, 129L, 73L, 18L, 3L, 8L, 15L, 43L, 44L, 30L, 6L)), sorted = "orderDate", class = c("data.table",
"data.frame"), row.names = c(NA, -12L), .internal.selfref = <pointer: 0x0000024e1b7d1ef0>)
however in the returnTest
table there there's a date row that's missing.
I tried merging using the productName
column as a key column but for some reason it kept giving me an error and that was the only way I could have merged both tables without an error. Ultimately, I want to have a data table to check the return rate of a certain product, but with this method I'm always missing a month where I could have orders but no returns or vice versa. Can anyone please help? I've been trying to solve this for about a week now.
test1 <- ordersByProductNameAndSize[`productName` == 'A. De La Sota Lady' ]
setkeyv(test1, 'orderDate')
test2 <- returnsByProductNameAndSize[`productName` == 'A. De La Sota Lady' ]
test2[, 'orderDate' := returnDate]
setkeyv(test2, 'orderDate'
returnTest <- merge(test1, test2[, c('orderDate', 'totalReturns'), all = TRUE, with = FALSE]) # , 'totalReturns'
returnTest[, 'returnRate' := ((totalReturns / totalOrders) *100)]