【问题标题】:Creating a count variable for number of years since a base observation为自基础观察以来的年数创建计数变量
【发布时间】:2018-01-21 09:40:08
【问题描述】:

我需要创建一个变量,告诉我自第一次观察特定组以来的年数,conflictID。我提供了一个示例数据集来说明我的问题。

conflictID <- c(205,205,205,209,209,221,221,221,221)
year <- c("1993", "1995", "1996", "1991", "1993", "2001", "2002", "2003", "2005")
df <- data.frame(conflictID, year)

这个数据框的输出是:

      conflictID year
1        205     1993
2        205     1995
3        205     1996
4        209     1991
5        209     1993
6        221     2001
7        221     2002
8        221     2003
9        221     2005

我想要这样的东西:

      conflictID year   duration
1        205     1993       0
2        205     1995       2
3        205     1996       3
4        209     1991       0
5        209     1993       2
6        221     2001       0
7        221     2002       1
8        221     2003       2
9        221     2005       4

其中 duration 变量对于每个 conflictid 的第一次观察为 0。基本上,如果有意义的话,我需要一种为每个 conflictID 的第一年设置基准日期的方法?

【问题讨论】:

    标签: r date dataframe datediff panel-data


    【解决方案1】:

    我们可以使用dplyr 库。 df2 是最终输出。

    library(dplyr)
    
    df2 <- df %>%
      mutate(year = as.numeric(as.character(year))) %>%
      group_by(conflictID) %>%
      mutate(duration = year - min(year))
    
    df2
    # A tibble: 9 x 3
    # Groups:   conflictID [3]
      conflictID  year duration
           <dbl> <dbl>    <dbl>
    1        205  1993        0
    2        205  1995        2
    3        205  1996        3
    4        209  1991        0
    5        209  1993        2
    6        221  2001        0
    7        221  2002        1
    8        221  2003        2
    9        221  2005        4
    

    请注意,您的year 列是factor 格式,这很难处理。我建议您在创建数据框时保持 numeric 格式的年份列。请看下面的例子。如果删除年份列中的引号。您的代码不需要mutate(year = as.numeric(as.character(year)))

    conflictID <- c(205,205,205,209,209,221,221,221,221)
    year <- c(1993, 1995, 1996, 1991, 1993, 2001, 2002, 2003, 2005)
    df <- data.frame(conflictID, year)
    

    【讨论】:

    • 我在示例数据集和我的原始数据集上都尝试过,但似乎都没有奏效。没有错误消息,但 duration 列中的值是错误的。我用数字重新创建了我的数据框,然后使用了您的代码,结果如下: conflictID year duration 1 205 1993 2 2 205 1995 4 3 205 1996 5 4 209 1991 0 5 209 1993 2 6 221 2001 10 7 221 2002 11 8 221 2003 12 9 221 2005 14
    • 抱歉,我没有意识到您无法将代码放入答案中,我是否应该在我自己的问题的单独答案中重现我的代码?我是 stackoverflow 的新手,所以不太了解协议!
    • group_by(conflictID)了吗?
    • 更新:我重写了将 dplyr:: 放在 mutate 和 group_by 函数之前的代码,这样就成功了!
    【解决方案2】:

    base R 中的一行...

    df$year <- as.numeric(as.character(df$year)) #your years are factors
    
    df$duration <- df$year - ave(df$year, df$conflictID, FUN=min)
    
    df
      conflictID year duration
    1        205 1993        0
    2        205 1995        2
    3        205 1996        3
    4        209 1991        0
    5        209 1993        2
    6        221 2001        0
    7        221 2002        1
    8        221 2003        2
    9        221 2005        4
    

    【讨论】:

      【解决方案3】:

      data.table中的另一条线

      library(data.table)
      setDT(df)[, duration := year - min(year), conflictID]
      df
      #   conflictID year duration
      #1:        205 1993        0
      #2:        205 1995        2
      #3:        205 1996        3
      #4:        209 1991        0
      #5:        209 1993        2
      #6:        221 2001        0
      #7:        221 2002        1
      #8:        221 2003        2
      #9:        221 2005        4
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-11-16
        • 2012-12-20
        • 1970-01-01
        • 2017-10-30
        • 2019-08-23
        • 2020-12-14
        • 1970-01-01
        相关资源
        最近更新 更多