Set column value using the first next row in the same group that meets a condition

前端 未结 3 843
予麋鹿
予麋鹿 2021-02-08 10:26

I am new to R and this is my first question on stackoverflow.

I am trying

  • to assign by reference to a new column
  • for each row
  • using the v
3条回答
  •  忘掉有多难
    2021-02-08 10:55

    A Not-the-Data-Table-Way approach:

    > df <- structure(list(
    +   id        = c(1L, 1L, 2L, 2L, 3L, 4L, 5L, 5L, 5L, 6L, 6L, 6L),
    +   code      = c("p", "f", "f", "p", "p", "", "f", "p", "p", "f", "p", "p"),
    +   date_down = structure(c(17897, 17898, 17898, 17899, 17900, 17901, 17903, 17903, 17905, 17906, 17906, 17906), class = "Date"),
    +   date_up   = structure(c(17898, 17899, 17898, NA, NA, 17901, 17904, 17904, 17905, 17906, 17906, 17907), class = "Date")),
    +   class     = c("data.frame"),
    +   row.names = c(NA, -12L))
    > 
    > 
    > Lista <- lapply(split(df, df$id), function(x){
    +   x$founddate <- 
    +     sapply(c(1:nrow(x)), function(y){
    +       na.omit(sapply(y:nrow(x), function(i){
    +         ifelse(x[i + 1, "code"] == "p" & x[i + 1, "date_up"] > x[y, "date_down"],
    +                x[i + 1, "date_up"], NA)
    +       }))[1]
    +     })
    +   x$founddate <- as.Date(x$founddate, origin = "1970-01-01")
    +   return(x)
    + })
    > 
    > 
    > df <- do.call(rbind.data.frame, Lista)
    > 
    > df
         id code  date_down    date_up  founddate
    1.1   1    p 2019-01-01 2019-01-02       
    1.2   1    f 2019-01-02 2019-01-03       
    2.3   2    f 2019-01-02 2019-01-02       
    2.4   2    p 2019-01-03              
    3     3    p 2019-01-04              
    4     4  2019-01-05 2019-01-05       
    5.7   5    f 2019-01-07 2019-01-08 2019-01-08
    5.8   5    p 2019-01-07 2019-01-08 2019-01-09
    5.9   5    p 2019-01-09 2019-01-09       
    6.10  6    f 2019-01-10 2019-01-10 2019-01-11
    6.11  6    p 2019-01-10 2019-01-10 2019-01-11
    6.12  6    p 2019-01-10 2019-01-11       
    > 
    

    Under the given conditions, there are more than one match per row. The proposed answer gets the first match, but this can be modified.

    Hope it helps.

提交回复
热议问题