I have a very large dataset (20+ years of telemetry data) and I am trying to understand if I am missing any days worth of data.
Using the example dataframe below:
dates <- rep(seq(as.Date("2004/01/01"), as.Date("2020/12/31"), "days"), each=4)
Animal_id <- rep(1:20, times=length(unique(dates)))
df <- data.frame(dates=dates, id=Animal_id)
df[,"Year"]<-format(df[,"dates"],"%Y")
This dataframe should be complete in that it has all the dates present, so if I remove some:
df <- df[-4, ]
df <- df[-6, ]
df <- df[-10, ]
df <- df[-15, ]
df <- df[-16, ]
df <- df[-20, ]
Now I want to figure out what dates I am missing and what animal ID these dates belong to. I have been using up to this point the following code:
FullSeqID <- seq.Date(from=min(df$dates), to=max(df$dates), by=1)
missingID <- FullSeqID[!FullSeqID %in% df$dates]
This will tell me what days I am missing data on, however I am trying to figure out what the ID is for the animal that is missing those days (and if it is just one that I can drop or multiple animals in which I will populate information for). Because of this I have also tried writing a loop to go through each year
df$combo<-paste(df$Animal_id,df$Year, sep = "-")
z<-split(df, df$combo)
missingID<-(NULL)
for (i in 1:length(z)){
AnimalIDloop<-z[[i]]
myDataDateID<-AnimalIDloop[,c("dates")]
FullSeqID<-seq.Date(from = min(myDataDateID), to=max(myDataDateID), by=1)
missingID<-rbind((FullSeqID[!FullSeqID %in% myDataDateID]),AnimalIDloop$Animal_id[2])
##rbind dataframe created with the column being AnimalID, Other columns = ##second part
##pull out first value from the AnimalID column
##missingID<-rbind(MissingID, new rows (above))
}
missingID
My goal is to use the rbind function to combine the dates I am missing from the FullSeqID with the corresponding ID column by taking out the first value of that column and putting this into the dataframe missingID created above.
This gives me the following output:
[1,] [2,]
Which is not what I am trying to get it to say, and can't figure out where I am going wrong with my rbind function. I have also used the following loop which has worked, but I am making the mistake of writing over the missingID so it only outputs the last year where there are missing dates.
for (i in 1:length(y)) {
AnimalIDloop <- y[[i]]
myDataDateID <- AnimalIDloop[, c("Date")]
FullSeqID <- seq.Date(from=min(myDataDateID), to=max(myDataDateID), by=1)
missingID <- FullSeqID[!FullSeqID %in% myDataDateID]
}
missingID