我正在尝试创建一个使用闪亮的不同商品每月价格的仪表板。但是,我的部分数据中包含如下不完整的信息:
Commodity Year Month Price
Tomatoes 2018 1 29
Tomatoes 2018 2 27
Tomatoes 2018 3 33
Tomatoes 2018 5 28
Tomatoes 2018 7 31
有些月份没有记录价格。因此,导致情节不完整。现在,我想向用户提示一条警告消息,指示所选商品的数据不完整。有什么办法吗?后续问题,当我通过某些数据进行分组时,是否有任何办法让我知道这种差异?谢谢!
答案 0 :(得分:0)
一个选择可能是当月数少于12时提示一条消息。
df <- data.frame(Commodity = rep("Tomatoes", 5),
Year = rep(2018, 5),
Month = c(1, 2, 3, 5, 7),
Price = c(19, 17, 33, 28, 31))
missing_months <- df %>%
group_by(Commodity, Year) %>%
# count the number of months for which you have data... assuming no NAs for Price
summarize(n_months_w_data = n()) %>%
filter(n_months_w_data < 12)
然后您可以使用missing_months
中的信息来提示何时/是否应该向用户发送警告消息(例如,如果nrow(missing_months) == 0
则不需要抛出警告)。