【发布时间】:2018-11-21 04:56:18
【问题描述】:
假设我是水果供应商,我有 2 张桌子。 1 用于购买和 1 用于销售,如下所示,
library(tibble)
library(tidyverse)
FruitBought <- tribble(
~name, ~Date, ~Qty,
"Apple", 20180101, 15,
"Apple", 20180105, 20,
"Banana", 20180102, 18,
"Banana", 20180109, 14
)
fruitSold <- tribble(
~Date, ~name, ~sold,
20180101, 'Apple', 5,
20180102, 'Apple', 3,
20180102, 'Banana', 3,
20180103, 'Apple', 1,
20180103, 'Banana', 4,
20180104, 'Apple', 2,
20180104, 'Banana', 2,
20180105, 'Apple', 1,
20180105, 'Banana', 2,
20180106, 'Apple', 2,
20180106, 'Banana', 3,
20180107, 'Apple', 2,
20180107, 'Banana', 1,
20180108, 'Apple', 0,
20180108, 'Banana', 3,
20180109, 'Apple', 2,
20180109, 'Banana', 1,
20180110, 'Apple', 3,
20180110, 'Banana', 1
)
我想获得每次购买的最后售罄日期。像这样。
name | Date | Qty | LastSoldOut
"Apple" | 20180101 | 15 | 20180107
"Apple" | 20180105 | 20 | NA
"Banana" | 20180102 | 18 | 20180109
"Banana" | 20180109 | 14 | NA
有人可以帮忙吗?
【问题讨论】:
-
选择
LastSoldOut日期的逻辑是什么?对于“Apple”,您的日期也大于“20180107” -
你需要为每个购买的物品提供一些唯一的标识符,否则对于系统来说一切都是一样的
-
@RonakShah,啊,我的意思是,我在 1 月 1 日买了 15 个苹果,我想知道那 15 个苹果什么时候卖完。
标签: r dplyr data.table