Search code examples
rcapture

R - Add 0 occurrences in capture-recapture data


I have capture-recapture data with variables Year, Date, ID, and Distance.moved such that:

> head(df)
Year  ID       Date     Distance.moved
2012 2012-005  29-05-12  10
2012 2012-006  30-05-12  22
2013 2013-001  22-06-13  5
2013 2013-002  23-06-13  6

I want to add 0 occurrences to the distance moved column, to get values for events where they did not move, such as:

Year  ID       Date     Distance.moved
2012 2012-005  29-05-12  10
2012 2012-005  30-05-12  0
2012 2012-006  29-05-12  0
2012 2012-006  30-05-12  22
2013 2013-001  22-06-13  5
2013 2013-002  22-06-13  0
2013 2013-002  23-06-13  6
2013 2013-001  23-06-13  0

So only adding 0s for dates that exist as events in the Date column, and have it grouped per year.

I tried to add a binary 1,0 column for occurrences as such:

df_occ <- df %>%
        group_by(Date,Year, ID) %>%
        summarize(occurrence=n()) %>%
        as.data.frame()

but this just gives me occurrence values = 1 for all IDs


Solution

  • We can use complete after grouping by 'Year'

    library(dplyr)
    library(tidyr)
    df1 %>%
        group_by(Year) %>%
        complete(ID, Date, fill = list(Distance.moved = 0))
    # A tibble: 8 x 4
    # Groups:   Year [2]
    #   Year ID       Date     Distance.moved
    #  <int> <chr>    <chr>             <dbl>
    #1  2012 2012-005 29-05-12             10
    #2  2012 2012-005 30-05-12              0
    #3  2012 2012-006 29-05-12              0
    #4  2012 2012-006 30-05-12             22
    #5  2013 2013-001 22-06-13              5
    #6  2013 2013-001 23-06-13              0
    #7  2013 2013-002 22-06-13              0
    #8  2013 2013-002 23-06-13              6
    

    data

    df1 <- structure(list(Year = c(2012L, 2012L, 2013L, 2013L), ID = c("2012-005", 
    "2012-006", "2013-001", "2013-002"), Date = c("29-05-12", "30-05-12", 
    "22-06-13", "23-06-13"), Distance.moved = c(10L, 22L, 5L, 6L)), 
    class = "data.frame", row.names = c(NA, 
    -4L))