Earliest Date for each id in R

前端 未结 4 1727
余生分开走
余生分开走 2020-12-31 18:54

I have a dataset where each individual (id) has an e_date, and since each individual could have more than one e_date, I\'m trying to get the earli

4条回答
  •  抹茶落季
    2020-12-31 19:14

    We can use data.table. Convert the 'data.frame' to 'data.table' (setDT(data_full)), grouped by 'id', we get the 1st row (head(.SD, 1L)).

    library(data.table)
    setDT(data_full)[order(e_date), head(.SD, 1L), by = id]
    

    Or using dplyr, after grouping by 'id', arrange the 'e_date' (assuming it is of Date class) and get the first row with slice.

    library(dplyr)
    data_full %>%
        group_by(id) %>%
        arrange(e_date) %>%
        slice(1L)
    

    If we need a base R option, ave can be used

    data_full[with(data_full, ave(e_date, id, FUN = function(x) rank(x)==1)),]
    

提交回复
热议问题