M Sandoval
M Sandoval

Reputation: 87

Is there a function to generate a sequence of number of days since the first date recorded in r?

I am trying to analyze the survival probability of a plant. So, I recorded every 8 days the survival/dead of each plant for 3 months. I have a dataframe like this:

   sampling date       ID    survival
1        1 04/03/2017 L4           0
2        1 04/03/2017 L5           0
3        1 04/03/2017 L6           0
4        2 15/03/2017 L4           0
5        2 15/03/2017 L5           1
6        2 15/03/2017 L6           0
7        3 23/03/2017 L4           0
8        3 23/03/2017 L6           1

Where: Survival is a binomial vector: 1= alive, 0= dead. ID= id of each plant. Date= date of sampling. I tried many different combinations using lubridate package in R, but I could not do it. I would like to obtain a dataframe like this:

   sampling date       ID    survival        days
1        1 04/03/2017 L4           0           0
2        1 04/03/2017 L5           0           0
3        1 04/03/2017 L6           0           0
4        2 15/03/2017 L4           0           11
5        2 15/03/2017 L5           1           11
6        2 15/03/2017 L6           0           11
7        3 23/03/2017 L4           0           19
8        3 23/03/2017 L6           1           19

Any suggestion? I will appreciate your help, thank you.

Upvotes: 1

Views: 823

Answers (3)

Ronak Shah
Ronak Shah

Reputation: 388982

Once you change the date to actual date class.

df$date <- as.Date(df$date, "%d/%m/%Y")

You can do this in base R :

df$days <- with(df, as.integer(date - ave(date, ID, FUN = min)))

dplyr

library(dplyr)
df %>% group_by(ID) %>% mutate(days = as.integer(date - min(date)))

Or in data.table

library(data.table)
setDT(df)[, days := as.integer(date - min(date)), ID]
df

#   sampling       date ID survival days
#1:        1 2017-03-04 L4        0    0
#2:        1 2017-03-04 L5        0    0
#3:        1 2017-03-04 L6        0    0
#4:        2 2017-03-15 L4        0   11
#5:        2 2017-03-15 L5        1   11
#6:        2 2017-03-15 L6        0   11
#7:        3 2017-03-23 L4        0   19
#8:        3 2017-03-23 L6        1   19

Upvotes: 1

Neel Kamal
Neel Kamal

Reputation: 1076

Using dplyr and lubridate

library(dplyr)
library(lubridate)

d %>% group_by(ID) %>% 
  mutate(days = difftime(time1 = date, time2 = min(date), units = "days"))

Output

sampling date       ID    survival days   
  <chr>    <date>     <chr>    <dbl> <drtn> 
1 1        2017-03-04 L4           0  0 days
2 1        2017-03-04 L5           0  0 days
3 1        2017-03-04 L6           0  0 days
4 2        2017-03-15 L4           0 11 days
5 2        2017-03-15 L5           1 11 days
6 2        2017-03-15 L6           0 11 days
7 3        2017-03-23 L4           0 19 days
8 3        2017-03-23 L6           1 19 days

Upvotes: 1

kaabre
kaabre

Reputation: 56

You need to use the package chron, convert the date to a date object using the as.Date function, and then the dates will be subtractable.

I get your desired output using this code:

Input:

## to create your sample dataset -- so you can see what data types I started with
sampling <- c(1,1,1,2,2,2,3,3)
date <- c('04/03/2017','04/03/2017','04/03/2017','15/03/2017','15/03/2017','15/\
03/2017','23/03/2017','23/03/2017')
ID <- c('L4','L5','L6','L4','L5','L6','L4','L6')
survival <- c(0,0,0,0,1,0,0,1)

##this is the actual part of the code that calculates days since the first

library(chron)
df <- data.frame(sampling,date,ID,survival)
df$date <- as.Date(df$date,format='%d/%m/%Y') 
# convert date to chron fmt -- see
#    https://www.stat.berkeley.edu/~s133/dates.html

df$days <- as.integer(df$date - df$date[1])

Output:

> df
  sampling       date ID survival days
1        1 2017-03-04 L4        0    0
2        1 2017-03-04 L5        0    0
3        1 2017-03-04 L6        0    0
4        2 2017-03-15 L4        0   11
5        2 2017-03-15 L5        1   11
6        2 2017-03-15 L6        0   11
7        3 2017-03-23 L4        0   19
8        3 2017-03-23 L6        1   19

Upvotes: 2

Related Questions