R 为自基本观测以来的年数创建计数变量

R 为自基本观测以来的年数创建计数变量,r,date,dataframe,datediff,panel-data,R,Date,Dataframe,Datediff,Panel Data,我需要创建一个变量,它告诉我从第一次观察到一个特定的组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以来的年数。我提供了一个示例数据集来说明我的问题

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   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

其中,对于每个冲突ID的第一次观察,持续时间变量为0。基本上,我需要的是一种为每个冲突ID的第一年设置基准日期的方法,如果这有意义的话?

我们可以使用
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
基线中的一行

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

df$year数据表中的另一行

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

我在样本数据集和原始数据集上都尝试过,但两种方法似乎都不起作用。没有错误消息,但持续时间列中的值错误。我重新创建了数据框,年份为数字,然后使用你的代码,结果是:冲突年份持续时间1201993 22052051995 432051996 542091991 052091993 622212001 1072212002 112822212003 1292212005 14抱歉我不知道你不能把代码放在答案中,我应该把代码放在我自己问题的单独答案中吗?我是stackoverflow的新手,所以不太了解协议!你是否
groupby(conflictID)
?更新:我将dplyr::放在mutate和groupby函数之前重新编写了代码,这就成功了!
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
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