R 如何更改数据框中的日期

R 如何更改数据框中的日期,r,R,我有很多数据帧,像这样 我必须改变第一个变量。如果ID first-second time>365,则第一个变量将再次更改 如果第一次第二次和第二次第三次>365,则第三次更改 比如 假设您上面的预期输出有一些错误,我认为这就是您所追求的 #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

我有很多数据帧,像这样

我必须改变第一个变量。如果ID first-second time>365,则第一个变量将再次更改 如果第一次第二次和第二次第三次>365,则第三次更改 比如


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

#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")
#首先,这里是可复制/粘贴形式的数据

dd您确定预期输出的值正确吗?与ID C一样,
first\u ee\t
值不应该是“20040224”吗?对于ID G,它不应该是“20020318”吗?@MrFlick OH~~ID C已经更正,ID G是真的。无论第一个变量还是第二个变量,如果第三个、第四个和第五个变量为NA,则第一个变量不为“0”OK。我在我的回答中增加了这个要求。现在可以接受吗?@flick先生非常感谢
 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
#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)