r - 在 R 中合并 2 个 data.tables,其中 1 个数据表有一个额外的行
问题描述
所以我一直在尝试合并这两个data.table
看起来像这样的
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")
和
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")
结果是合并的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>)
但是在returnTest
表格中缺少一个日期行。
我尝试使用该productName
列作为键列进行合并,但由于某种原因,它一直给我一个错误,这是我可以合并两个表而没有错误的唯一方法。最终,我希望有一个数据表来检查某个产品的退货率,但是使用这种方法,我总是会错过一个月,我可以有订单但没有退货,反之亦然。有人可以帮忙吗?我已经尝试解决这个问题大约一个星期了。
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)]
解决方案
感谢您发布您的数据!如果我正确理解这一点,您的“缺失”值只是在 2020 年 6 月 1 日订购了一件物品,但在该日期没有返回任何东西的情况,对吗?
t1 <- 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"))
t2 <- 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"))
rt <- merge(t1, t2, by = "orderDate", all = TRUE)
# calculate return rate
rt$returnRate <- (rt$totalReturns / rt$totalOrders) * 100
推荐阅读
- python - Python 嗅探器——从包中提取信息的方法
- haskell - 自然数的初始代数
- android - Kotlin:检查多个布尔函数的惯用方法
- python - Python ctypes dll调用
- jenkins - 当这两个文件在不同的作业中时,如何将参数从 Jenkins 文件传递到 Groovy 脚本文件
- javascript - 无法从 vue 项目中删除依赖项
- java - 无法在 Google Fit Api 中累积过去一周的心率数据
- python - 在python中使用json中的符号
- testing - 当页面对象中声明的断言在 TestCafe 的同一测试中被调用两次时,DOM 快照警告
- rest - 谷歌云存储与谷歌云存储 JSON API