如何更改数据框中的日期

时间:2014-05-17 13:40:53

标签: r

  

我有很多数据框,比如这个

ID      1       2       3         4       5         type        c new_ee   first_ee_t   
 A  20051110 20051111 20051114 20051208 20060105     DATE       1 none        none     
 A     NA       1       3        24       2        diff_date    2   1       20051110
 B  20050422 20050613 20050711 20071023   NA         DATE       1 none        none
 B     NA      52      28        834     999       diff_date    2   1       20050422
 C  20021206 20040224 20040423 20040507 20040528     DATE       1 none        none
 C     NA      445     59        14       21       diff_date    2   1       20021206
 D  20030708 20050228 20050228 20050815 20050915     DATE       1 none        none
 D     NA      601      0       168       31       diff_date    2   1       20030708
 E  20000123 20040306 20060919 20060919 20060920     DATE       1 none        none  
 E     NA     1504     927        0        1       diff_date    2   1       20000123
 F  20070413   NA       NA        NA       NA        DATE       1 none        none
 F     NA     999      999       999      999      diff_date    2   0          0
 G  20020318 20020411   NA        NA       NA        DATE       1 none        none
 G     NA      24      999       999      999      diff_date    2   0          0

我必须更改first_ee_t变量。如果ID第一次 - 第二次> 365则first_ee_t变量第二次改变 如果第一 - 第二时间和第二 - 第三时间> 365,则改变第三时间 比如

 ID     1       2       3         4       5         type        c new_ee   first_ee_t   
 A  20051110 20051111 20051114 20051208 20060105     DATE       1 none        none     
 A     NA       1       3        24       2        diff_date    2   1       20051110
 B  20050422 20050613 20050711 20071023   NA         DATE       1 none        none
 B     NA      52      28        834     999       diff_date    2   1       20050422
 C  20021206 20040224 20040423 20040507 20040528     DATE       1 none        none
 C     NA      445     59        14       21       diff_date    2   1       20040224
 D  20030708 20050228 20050228 20050815 20050915     DATE       1 none        none
 D     NA      601      0       168       31       diff_date    2   1       20050228
 E  20000123 20040306 20060919 20060919 20060920     DATE       1 none        none  
 E     NA     1504     927        0        1       diff_date    2   1       20060919
 F  20070413   NA       NA        NA       NA        DATE       1 none        none
 F     NA     999      999       999      999      diff_date    2   0          0
 G  20020318 20020411   NA        NA       NA        DATE       1 none        none
 G     NA      24      999       999      999      diff_date    2   0          0

1 个答案:

答案 0 :(得分:1)

假设您上面的预期输出有一些错误,我认为这就是您的目标

#first, here's the data in a copy/paste-able form
dd <-
structure(list(ID = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 
5L, 5L, 6L, 6L, 7L, 7L), .Label = c("A", "B", "C", "D", "E", 
"F", "G"), class = "factor"), X1 = c(20051110L, NA, 20050422L, 
NA, 20021206L, NA, 20030708L, NA, 20000123L, NA, 20070413L, NA, 
20020318L, NA), X2 = c(20051111L, 1L, 20050613L, 52L, 20040224L, 
445L, 20050228L, 601L, 20040306L, 1504L, NA, 999L, 20020411L, 
24L), X3 = c(20051114L, 3L, 20050711L, 28L, 20040423L, 59L, 20050228L, 
0L, 20060919L, 927L, NA, 999L, NA, 999L), X4 = c(20051208L, 24L, 
20071023L, 834L, 20040507L, 14L, 20050815L, 168L, 20060919L, 
0L, NA, 999L, NA, 999L), X5 = c(20060105L, 2L, NA, 999L, 20040528L, 
21L, 20050915L, 31L, 20060920L, 1L, NA, 999L, NA, 999L), type = structure(c(1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("DATE", 
"diff_date"), class = "factor"), c = c(1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), new_ee = structure(c(3L, 2L, 
3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 1L, 3L, 1L), .Label = c("0", 
"1", "none"), class = "factor"), first_ee_t = c("none", "20051110", 
"none", "20050422", "none", "20021206", "none", "20030708", "none", 
"20000123", "none", "0", "none", "0")), .Names = c("ID", "X1", 
"X2", "X3", "X4", "X5", "type", "c", "new_ee", "first_ee_t"), row.names = c(NA, 
-14L), class = "data.frame")

以及如何进行转换的代码

result<-unsplit(lapply(split(dd, dd$ID), function(x) {
    if (all(is.na(x[1,4:6]))) {
        x[2, "first_ee_t"]<-0
    } else {
        first<-min(which(x[2,2:6]<365))
        if(is.finite(first)) {
            x[2,"first_ee_t"]<-x[1, first]
        }       
    }
    x
}), dd$ID)

这假设每个ID只有两行,第二行总是包含日期,第一行总是包含日期。

在ID F的情况下,这确实产生了警告,它似乎没有满足要求的值,因此保持行不受影响。