【问题标题】:Add new row if column value exist如果列值存在,则添加新行
【发布时间】:2020-11-06 18:21:58
【问题描述】:

我有以下名为 table.data 的 data.table 对象:

    month.option      brand som.month avg.sales distribution
1:          last SUPERBRAND 0.8860000  65.37234    0.9550000
2:      previous SUPERBRAND 0.8930000  65.31717    0.9710000
3: previous.year SUPERBRAND 0.9040000  66.16728    0.9710000
4:      all.year SUPERBRAND 0.9064906  67.50559    0.9710189

然后我执行以下代码来添加新行并获得与上一年的差异:

table.data %>%
          select(month.option, som.month) %>%
          add_row(
            month.option = "diff",
            som.month = (filter(., month.option == "last") %>% pull()) - (filter(., month.option == "previous") %>% pull())
          ) %>%
          filter(!month.option %in% c("last", "previous")) %>%
          mutate(som.month = percent(som.month, accuracy = 0.1)) %>%
          data.table::transpose(., make.names = 'month.option') %>%
          select(diff, everything()) %>%
          set_names(c("Dif vs MA", "SOM YTD", "SOM AA"))

但是我遇到了以下情况:

    month.option      brand som.month avg.sales distribution
1:      all.year SUPERBRAND 0.9064906  67.50559    0.9710189

前面的代码抛出错误的地方

Internal error in `vec_assign()`: `value` should have been recycled to fit `x`.

我尝试了几种方法来弥补这种情况,其中一种如下:

table.data %>%
  select(month.option, som.month) %>%
  add_row(
    month.option = "diff",
    ifelse(unique(month.option) != 'all.year', som.month = (filter(., month.option == "last") %>% pull()) - (filter(., month.option == "previous") %>% pull()), "Not enough data")
  ) %>%
          filter(!month.option %in% c("last", "previous")) %>%
          mutate(som.month = percent(som.month, accuracy = 0.1)) %>%
          data.table::transpose(., make.names = 'month.option') %>%
          select(diff, everything()) %>%
          set_names(c("Dif vs MA", "SOM YTD", "SOM AA"))

但它仍然会引发错误。我想知道是否有人知道我如何实现一个过滤器来检查我在month.option 中是否有值“last”和“previous”然后它执行第一个代码,如果没有它只显示文本“not enough Data”?

【问题讨论】:

  • 请将数据集添加为可重现的对象。使用dput(head(table.data)) 并共享该命令的输出。
  • 内部错误应该报告给项目存储库,因为它们不应该对用户可见

标签: r dplyr data.table


【解决方案1】:

也许是这样的:

# check condition
if("last" %in% table.data$month.option
   && "previous" %in% table.data$month.option){
  print("not enough data")
} else {
  # carry on with rest of calculation as per original code
  ...
}

Alternatively, you might find the `complete` function useful ([reference][1]). If you have a full table containing all your month.options and brands then this can be used to create all the missing rows and give them default values.

  [1]: https://tidyr.tidyverse.org/reference/complete.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-02
    • 2019-03-12
    • 2014-07-04
    • 1970-01-01
    • 2021-08-27
    • 1970-01-01
    相关资源
    最近更新 更多