vendredi 4 mai 2018

check if list of dates is within a date range in r

There are three main columns to my data shown below (squirrel_id = unique individual id, byear = year of birth, and dyear = year of death):

> summary(complete)
  squirrel_id        byear          dyear     
 Min.   :  416   Min.   :1989   Min.   :1989  
 1st Qu.: 4152   1st Qu.:1997   1st Qu.:1998  
 Median : 7870   Median :2003   Median :2004  
 Mean   :10419   Mean   :2004   Mean   :2004  
 3rd Qu.:16126   3rd Qu.:2011   3rd Qu.:2012  
 Max.   :23327   Max.   :2017   Max.   :2017 

I have a second piece of data (shown below) that I am trying to incorporate into the above dataset.

mast.yr<-c("1993", "1998", "2005", "2010", "2014")

I am trying to do two things:

  1. Add a column that says if the individual (squirrel_id) was alive during any of the mast.yr years (dyear- byear = range of years alive (including byear and dyear).
  2. Add another column that counts how many mast.yr years each individual (squirrel_id) experienced during lifetime (dyear- byear = range of years alive (including byear and dyear).

To generate the first column, I've been using the mutate function in the dplyr package, but I can only get it to work for the byear and dyear separately, like so:

complete <- complete %>%
    mutate (mast = ifelse (byear %in% c("1993", "1998", "2005", "2010", "2014"), 1, 0), 
    mast = ifelse (dyear %in% c("1993", "1998", "2005", "2010", "2014"), 1, 0))) 

But it doesn't give the desired output since it is considering the byear and dyear on their own, rather than as a continuous time period. I have tried the solutions posted here and here, but have had no luck.

Any suggestions would be appreciated!

A copy of my data can be found here.

Aucun commentaire:

Enregistrer un commentaire