汇总周数并修改从列中检索到的 headers

Aggregate weeks and modify headers retrieved from column

我正在尝试每周汇总数据。

| Timestamp     |     District   |
|   --------    | -------------- |
| 2015-01-16    | Kreuzberg      |
| 2015-01-10    | Charlottenburg |
| 2015-02-09    | Mitte          |
| 2014-10-10    | Lichtenberg    |

每个时间戳代表一个病人。我想要实现的是将地区设置为 headers 并将时间戳汇总为数周。重要的是要了解每个地区每周有多少人感染。

|     Week      | Kreuzberg | Charlottenburg | Mitte | Lichtenberg
|   --------    | ----------|   ----------   | ----- | ----------
| 2015-01-16    |     1     |         0      |    0  |     0 
| 2015-01-10    |     0     |         1      |    0  |     0
| 2015-02-09    |     0     |         0      |    1  |     0
| 2014-10-10    |     0     |         0      |    0  |     1

到目前为止,我有以下代码,但没有提供我需要的结果。

new_df <-
  df %>% 
  drop_na(Timestamp) %>%             
  mutate(week = floor_date(   
    Timestamp,
    unit = "week")) %>% 
  count(week, District) 

有什么建议吗?

最好的, 丹尼尔

根据您的代码,您可以通过管道传递一个 pivot_wider 函数:

library(dplyr)
library(tidyr)
library(lubridate)

df %>% 
  drop_na(Timestamp) %>%             
  mutate(week = floor_date(   
    Timestamp,
    unit = "week")) %>% 
  count(week, District) %>% 
  pivot_wider(names_from = District, values_from = n, values_fill = 0)

这个returns

# A tibble: 4 x 5
  week       Lichtenberg Charlottenburg Kreuzberg Mitte
  <date>           <int>          <int>     <int> <int>
1 2014-10-05           1              0         0     0
2 2015-01-04           0              1         0     0
3 2015-01-11           0              0         1     0
4 2015-02-08           0              0         0     1