【问题标题】:Accumulating by date按日期累积
【发布时间】:2019-07-03 11:57:02
【问题描述】:

我想知道某一天会有多少动物出现。这张图表描述了人们提前登记他们的动物。

例如,在7 几天前,有人注册了他们的4 猫以出现在5/3/2019 上;在6 几天前,另一只9 猫注册了5/3/2019。所以7+6=13 猫会出现在5/3/2019 上。

days_ahead = 0 时,仅表示有人在活动当天注册。例如,4 wolves 在5/1/2019 上注册了5/1/2019(提前0 天),那一天将会有4 wolves。

library(dplyr)
set.seed(0)

animal = c(rep('cat', 5), rep('dog', 6), rep('wolf', 3))
date = sample(seq(as.Date("2019/5/1"), as.Date('2019/5/10'), by='day'), 14, replace=TRUE)
days_ahead = sample(seq(0,14), 14, replace=FALSE)
number = sample.int(10, 14, replace=TRUE)

dt = data.frame(animal, date, days_ahead, number) %>% arrange(animal, date)

预期结果应该具有与示例相同的1-3 列,但第四列应该是每个date 的累加数,在days_ahead 上累加。


我在这里添加了一个预期的结果。 comments 用于解释accumulated_number 列。

我考虑过loop 函数,但不完全确定如何循环三个变量(cat、date 和 days_ahead)。任何建议表示赞赏!

【问题讨论】:

  • 问题不是很清楚,部分原因是文中的描述与示例数据不符。此外,尚不清楚日期和未来天数之间的关系 - 是否应从前者中减去后者以给出注册日期?这将有助于改进示例数据。
  • 感谢您的 cmets!我添加了预期的输出。
  • 使用基础 R ave 你可以做到with(dt, ave(number, animal, date, FUN = cumsum))

标签: r loops for-loop dplyr aggregate


【解决方案1】:

accumulated_number 使用cumsum() 有点简单。请参阅此链接以获取您的 comments 字段:

Cumulatively paste (concatenate) values grouped by another variable

dt%>%
  group_by(animal,date)%>%
  mutate(accumulated_number = cumsum(number)
         ,comments = Reduce(function(x1, x2) paste(x1, x2, sep = '+'), as.character(number), accumulate = T)
         )%>%
  ungroup()

此外,我的数据集与您使用相同种子的数据集略有不同。不过,它似乎有效。

# A tibble: 14 x 6
   animal date       days_ahead number accumulated_number comments
   <fct>  <date>          <int>  <int>              <int> <chr>   
 1 cat    2019-05-03         10      9                  9 9       
 2 cat    2019-05-04          6      4                  4 4       
 3 cat    2019-05-06          8      5                  5 5       
 4 cat    2019-05-09          5      4                  4 4       
 5 cat    2019-05-10         13      6                  6 6       
 6 dog    2019-05-01          0      2                  2 2       
 7 dog    2019-05-03          3      5                  5 5       
 8 dog    2019-05-07          1      7                  7 7       
 9 dog    2019-05-07          9      8                 15 7+8     
10 dog    2019-05-09         12      2                  2 2       
11 dog    2019-05-10          7      9                  9 9       
12 wolf   2019-05-02         14      5                  5 5       
13 wolf   2019-05-03         11      8                  8 8       
14 wolf   2019-05-07          4      9                  9 9 

【讨论】:

  • 我认为 OP 不需要comments 列,仅用于解释。只是cumsum 分组。
  • @Ronak 我认为你是对的。不过,comments 的研究很有趣。我把它放在里面。
【解决方案2】:

我不确定我是否理解你的问题,这是你想要的吗?

我正在添加一个“animals_arriving”列并保留dt的其余部分

library(dplyr)
library(lubridate)
dt %>% 
  mutate(date_arrival = date + days(days_ahead)) %>%
  group_by(date = date_arrival) %>% 
  summarise(animals_arriving = n()) %>% 
  full_join(dt,by="date")

【讨论】:

  • 嗨 Fino,感谢您的回答。我运行了您的代码,但我对结果并不完全确定 - 似乎第 13 行(2019 年 5 月 3 日)有 NA 动物到达,但有猫来了。我想知道每个date 上会有多少只猫/狗/狼。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-15
  • 2014-04-12
  • 2021-06-09
  • 1970-01-01
  • 2021-06-04
相关资源
最近更新 更多