R data.table binary value for last row in group by condition

前端 未结 5 1989
感情败类
感情败类 2021-01-18 00:15

I have data like this:

library(data.table)
id <- c(\"1232\",\"1232\",\"1232\",\"4211\",\"4211\",\"4211\")
conversion <- c(0,0,0,1,1,1)
DT <- data.ta         


        
5条回答
  •  無奈伤痛
    2021-01-18 01:10

    Have you tried something like the following?

    library(tidyverse)
    
    final_conversion_dat <- DT %>% 
      group_by(id) %>% 
      mutate(date = as.Date(date),
             final_conversion = ifelse(date == max(date, na.rm = T) & conversion == 1, 1, 0))
    

提交回复
热议问题