In case you don't already have data.table
install.packages("data.table","http://cran.us.r-project.org")
library (data.table)
I've imported the data in this way:
dt <- fread("pathToData.csv",key='Camera,Site,SpeciesID')
but I'm putting a sample of 10 observations on SO. Feel free to say so if you want them removed.
df <- structure(list(Individual = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L), Date = c(15543L, 15543L, 15543L, 15554L, 15554L, 15554L,
15554L, 15554L, 15543L, 15543L), Image1 = c("544.1.P5_", "544.1.P5_",
"544.1.P5_", "544.7.I1_2", "544.7.I1_2", "544.7.I1_2", "544.7.I1_2",
"544.7.I1_2", "544.1.P5_", "544.1.P5_"), Site = c("544", "544",
"544", "544", "544", "544", "544", "544", "544", "544"), Camera = c(1L,
1L, 1L, 7L, 7L, 7L, 7L, 7L, 1L, 1L), Plot = c(1L, 1L, 1L, 5L,
5L, 5L, 5L, 5L, 1L, 1L), Plot_Type = c("OnTrail", "OnTrail",
"OnTrail", "OffTrail", "OffTrail", "OffTrail", "OffTrail", "OffTrail",
"OnTrail", "OnTrail"), CameraID = c("P5", "P5", "P5", "I1", "I1",
"I1", "I1", "I1", "P5", "P5"), Time = c("2012/07/22 00:31:00",
"2012/07/22 00:31:00", "2012/07/22 00:31:00", "2012/08/02 09:09:00",
"2012/08/02 09:09:00", "2012/08/02 09:09:00", "2012/08/02 09:09:00",
"2012/08/02 09:09:00", "2012/07/22 00:31:00", "2012/07/22 00:31:00"
), Hour = c(0L, 0L, 0L, 9L, 9L, 9L, 9L, 9L, 0L, 0L), Minute = c(31L,
31L, 31L, 9L, 9L, 9L, 9L, 9L, 31L, 31L), Second = c(17L, 18L,
23L, 18L, 20L, 22L, 24L, 26L, 34L, 36L), SpeciesID = c(2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), Common = c("Mule deer", "Mule deer",
"Mule deer", "Mule deer", "Mule deer", "Mule deer", "Mule deer",
"Mule deer", "Mule deer", "Mule deer"), Scientific = c("Odocoile",
"Odocoile", "Odocoile", "Odocoile", "Odocoile", "Odocoile", "Odocoile",
"Odocoile", "Odocoile", "Odocoile"), SpeciesID.1 = c(2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L)), .Names = c("Individual", "Date",
"Image1", "Site", "Camera", "Plot", "Plot_Type", "CameraID",
"Time", "Hour", "Minute", "Second", "SpeciesID", "Common", "Scientific",
"SpeciesID.1"), row.names = c(NA, 10L), class = "data.frame")
dt = data.table(df,key='Camera,Site,SpeciesID')
# 1) Count the number of obs per category (SpeciesID,Cameraand Site)
# 2) Convert Time into time variable (the format is optional here)
# 3) Perform the time difference where there were at least 2 observations per category
# (SpeciesID,Cameraand Site) and compute the time difference for each category.
# (in seconds)
dt[,n:=.N,list(SpeciesID,Camera,Site)][
,Time:=as.POSIXct(Time,format="%Y/%m/%d %H:%M:%S")][
n>2,diffSec:=filter(Time,c(1,-1),sides=1),list(Camera,Site,SpeciesID)]
Other Solution
dt[,n:=.N,list(SpeciesID,Camera,Site)][
,Time:=as.POSIXct(Time,format="%Y/%m/%d %H:%M:%S")][
n>2,diffSec:=c(NA,diff(Time,1)),list(Camera,Site,SpeciesID)]
You may want to check your variable Time format as well. Is it the right time? I've noticed that seconds were not included.