【问题标题】:How to change from event data to time-series cross-sectional data with event dummies?如何使用事件假人将事件数据更改为时间序列横截面数据?
【发布时间】:2018-03-21 18:04:47
【问题描述】:

我得到了一个数据集,该数据集按以下方式按国家/地区列出了特定事件的日期。

country date1 date2   
1    03/01/2012    05/01/2012
2    05/04/2012    12/10/2012
3    07/12/2012    20/03/2012
4    04/02/2012    24/12/2012

我需要使用这些数据为国家/年/月/日级别创建面板数据。我想为每个事件创建一个虚拟变量。

country year month   day
1  2012    01    01
1  2012    01    02
1  2012    01    03
1  2012    01    04
1  2012    01    05
1  2012    01    06

最终结果如下所示,每个国家/地区面板在每个单独的事件变量中的每个年/月/日都有 0 或 1。

country year month day event1 event2 
1  2012    01    01    0    0 
1  2012    01    02    0    0
1  2012    01    03    1    0
1  2012    01    04    1    0
1  2012    01    05    1    1
1  2012    01    06    1    1

问题是如何最有效地从我拥有的数据中获取我需要的数据结构。我发现了一个以前的问题有一个类似的问题(Dummy Variable by date.),但是这个问题没有处理面板数据。

【问题讨论】:

    标签: r datetime transformation dummy-variable


    【解决方案1】:

    这是一个tidyverse 解决方案。这个想法是使用tidyr::complete 来生成您想要的全套日期-国家/地区组合。然后很容易使用tidyr::spreadhas_event 的值拆分为每个事件的单独列,并使用sep 参数创建正确的列名称。其余的只是清理 - 将日期转换为单独的 yearmonthday 列,删除无关的列,并将事件列中的 NA 替换为 0。这对于更多的国家、每个国家/地区的更多事件或较大的日期范围应该是稳健的。

    library(tidyverse)
    library(lubridate)
    #> 
    #> Attaching package: 'lubridate'
    #> The following object is masked from 'package:base':
    #> 
    #>     date
    tbl <- read_table2(
      "country date1 date2
      1    03/01/2012    05/01/2012
      2    05/04/2012    12/10/2012
      3    07/12/2012    20/03/2012
      4    04/02/2012    24/12/2012"
    )
    #> Warning in rbind(names(probs), probs_f): number of columns of result is not
    #> a multiple of vector length (arg 2)
    #> Warning: 1 parsing failure.
    #> row # A tibble: 1 x 5 col     row col   expected actual        file         expected   <int> <chr> <chr>    <chr>         <chr>        actual 1     4 date2 ""       embedded null literal data file # A tibble: 1 x 5
    
    tbl %>%
      gather(event, date, date1:date2) %>%
      mutate(date = dmy(date)) %>%
      complete(country, date = seq.Date(min(date), max(date), 1)) %>%
      mutate(
        event = str_remove_all(event, "date"),
        has_event = ifelse(is.na(event), 0, 1)
        ) %>%
      spread(event, has_event, sep = "") %>%
      mutate_at(vars(event1:event2), replace_na, 0) %>%
      mutate(
        year = year(date),
        month = month(date),
        day = day(date)
      ) %>%
      select(country, year:day, event1:event2)
    #> # A tibble: 1,428 x 6
    #>    country  year month   day event1 event2
    #>      <int> <dbl> <dbl> <int>  <dbl>  <dbl>
    #>  1       1 2012.    1.     3     1.     0.
    #>  2       1 2012.    1.     4     0.     0.
    #>  3       1 2012.    1.     5     0.     1.
    #>  4       1 2012.    1.     6     0.     0.
    #>  5       1 2012.    1.     7     0.     0.
    #>  6       1 2012.    1.     8     0.     0.
    #>  7       1 2012.    1.     9     0.     0.
    #>  8       1 2012.    1.    10     0.     0.
    #>  9       1 2012.    1.    11     0.     0.
    #> 10       1 2012.    1.    12     0.     0.
    #> # ... with 1,418 more rows
    

    reprex package (v0.2.0) 于 2018 年 3 月 21 日创建。

    【讨论】:

      猜你喜欢
      • 2021-01-16
      • 1970-01-01
      • 2012-12-11
      • 1970-01-01
      • 2021-12-18
      • 2016-03-04
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      相关资源
      最近更新 更多