Calculate Difference between dates by group in R

后端 未结 2 1836
忘掉有多难
忘掉有多难 2020-12-11 22:46

I\'m using a logistic exposure to calculate hatching success for bird nests. My data set is quite extensive and I have ~2,000 nests, each with a unique ID (\"ClutchID). I

2条回答
  •  死守一世寂寞
    2020-12-11 23:38

    Here is a similar solutions if you look for a difftime results in days, from a vector date, without NA values produce in the new column, and if you expect to group by several conditions/groups.

    make sure that your vector of date as been converting in the good format as previously explained.

    dat2 <- dat %>% 
    select(group1, group2, date) %>% 
    arrange(group1, group2, date) %>% 
    group_by(group1, group2) %>% 
    mutate(diff_date = c(0,diff(date)))
    

提交回复
热议问题