在数据框中生成日期序列,每个重复数据组具有可变长度

时间:2018-06-12 19:42:30

标签: r date seq

我有一个数据框,并根据两个日期之间的差异重复行:日期(样本收集日期)和孵化(出生日期)使用以下代码。差异(基本上是几个月的寿命)从4-12个月不等:

library(zoo)
test$difference <- 12 * as.numeric(as.yearmon(test$Date) - as.yearmon(test$hatch))

test$difference <- ceiling(test$difference)
test2 <- test[rep(row.names(test), test$difference),]

我需要从孵化日期开始为每个样本创建一个序列(每个样本都有一个唯一的序列号,sn),以1个月为增量增加,长度等于差异列中的值。我尝试了一个循环,但我无法弄清楚如何对它进行说明,因为我对R.很新。

目前日期格式为%Y-%m-%d,但我只对月份和年份分组感兴趣。

任何见解都非常有用:)

    structure(list(Sex = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("F", "J", "M"), class = "factor"), 
    Maturity = c(5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
    5L, 5L, 5L, 5L), XLength = c(12, 12, 12, 12, 12, 12, 12, 
    12, 12, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5), Weight = c(44.1, 
    44.1, 44.1, 44.1, 44.1, 44.1, 44.1, 44.1, 44.1, 73.6, 73.6, 
    73.6, 73.6, 73.6, 73.6), Ringcount = c(232L, 232L, 232L, 
    232L, 232L, 232L, 232L, 232L, 232L, 225L, 225L, 225L, 225L, 
    225L, 225L), Date = structure(c(10480, 10480, 10480, 10480, 
    10480, 10480, 10480, 10480, 10480, 10480, 10480, 10480, 10480, 
    10480, 10480), class = "Date"), hatch = structure(c(10248, 
    10248, 10248, 10248, 10248, 10248, 10248, 10248, 10248, 10255, 
    10255, 10255, 10255, 10255, 10255), class = "Date"), sn = c(1, 
    1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2), difference = c(9, 
    9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9)), .Names = c("Sex", 
"Maturity", "XLength", "Weight", "Ringcount", "Date", "hatch", 
"sn", "difference"), row.names = c(NA, 15L), class = "data.frame")

1 个答案:

答案 0 :(得分:1)

1)假设您要使用test添加包含日期的新列,我们可以编写以下内容。它不使用任何包。

Seq <- function(h) seq(h[1], length = length(h), by = "month")
transform(test, dates = ave(hatch, sn, FUN = Seq))

,并提供:

   Sex Maturity XLength Weight Ringcount       Date      hatch sn difference      dates
1    M        5    12.0   44.1       232 1998-09-11 1998-01-22  1          9 1998-01-22
2    M        5    12.0   44.1       232 1998-09-11 1998-01-22  1          9 1998-02-22
3    M        5    12.0   44.1       232 1998-09-11 1998-01-22  1          9 1998-03-22
4    M        5    12.0   44.1       232 1998-09-11 1998-01-22  1          9 1998-04-22
5    M        5    12.0   44.1       232 1998-09-11 1998-01-22  1          9 1998-05-22
6    M        5    12.0   44.1       232 1998-09-11 1998-01-22  1          9 1998-06-22
7    M        5    12.0   44.1       232 1998-09-11 1998-01-22  1          9 1998-07-22
8    M        5    12.0   44.1       232 1998-09-11 1998-01-22  1          9 1998-08-22
9    M        5    12.0   44.1       232 1998-09-11 1998-01-22  1          9 1998-09-22
10   M        5    16.5   73.6       225 1998-09-11 1998-01-29  2          9 1998-01-29
11   M        5    16.5   73.6       225 1998-09-11 1998-01-29  2          9 1998-03-01
12   M        5    16.5   73.6       225 1998-09-11 1998-01-29  2          9 1998-03-29
13   M        5    16.5   73.6       225 1998-09-11 1998-01-29  2          9 1998-04-29
14   M        5    16.5   73.6       225 1998-09-11 1998-01-29  2          9 1998-05-29
15   M        5    16.5   73.6       225 1998-09-11 1998-01-29  2          9 1998-06-29

2)或者,如果您只想要年月和月份,请使用动物园的yearmon课程。

library(zoo)

Seq_ym <- function(h) h[1] + (seq_along(h) - 1) / 12
transform(test, dates = ave(as.yearmon(hatch), sn, FUN = Seq_ym))

,并提供:

   Sex Maturity XLength Weight Ringcount       Date      hatch sn difference    dates
1    M        5    12.0   44.1       232 1998-09-11 1998-01-22  1          9 Jan 1998
2    M        5    12.0   44.1       232 1998-09-11 1998-01-22  1          9 Feb 1998
3    M        5    12.0   44.1       232 1998-09-11 1998-01-22  1          9 Mar 1998
4    M        5    12.0   44.1       232 1998-09-11 1998-01-22  1          9 Apr 1998
5    M        5    12.0   44.1       232 1998-09-11 1998-01-22  1          9 May 1998
6    M        5    12.0   44.1       232 1998-09-11 1998-01-22  1          9 Jun 1998
7    M        5    12.0   44.1       232 1998-09-11 1998-01-22  1          9 Jul 1998
8    M        5    12.0   44.1       232 1998-09-11 1998-01-22  1          9 Aug 1998
9    M        5    12.0   44.1       232 1998-09-11 1998-01-22  1          9 Sep 1998
10   M        5    16.5   73.6       225 1998-09-11 1998-01-29  2          9 Jan 1998
11   M        5    16.5   73.6       225 1998-09-11 1998-01-29  2          9 Feb 1998
12   M        5    16.5   73.6       225 1998-09-11 1998-01-29  2          9 Mar 1998
13   M        5    16.5   73.6       225 1998-09-11 1998-01-29  2          9 Apr 1998
14   M        5    16.5   73.6       225 1998-09-11 1998-01-29  2          9 May 1998
15   M        5    16.5   73.6       225 1998-09-11 1998-01-29  2          9 Jun 1998