I see a few problems with your code. The function cam.days refers to columns End.Date and Start.Date but your sample data has columns named Camera.End.Date.and.Time and Camera.Start.Date.and.Time. Those columns are characters and will not work with difftime(). They need to be transformed to numeric timestamps.
This version of your code seems to work.
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
yr2009 <- structure(list(Genus = c("Atilax", "Atilax", "Atilax", "Atilax",
"Atilax", "Atilax", "Atilax", "Atilax", "Atilax", "Bdeogale",
"Bdeogale", "Bdeogale", "Bdeogale", "Bdeogale", "Bdeogale", "Bdeogale",
"Bdeogale", "Bdeogale", "Bdeogale", "Bdeogale"), Species = c("paludinosus",
"paludinosus", "paludinosus", "paludinosus", "paludinosus", "paludinosus",
"paludinosus", "paludinosus", "paludinosus", "crassicauda", "crassicauda",
"crassicauda", "crassicauda", "crassicauda", "crassicauda", "crassicauda",
"crassicauda", "crassicauda", "crassicauda", "crassicauda"),
Class = c("MAMMALIA", "MAMMALIA", "MAMMALIA", "MAMMALIA",
"MAMMALIA", "MAMMALIA", "MAMMALIA", "MAMMALIA", "MAMMALIA",
"MAMMALIA", "MAMMALIA", "MAMMALIA", "MAMMALIA", "MAMMALIA",
"MAMMALIA", "MAMMALIA", "MAMMALIA", "MAMMALIA", "MAMMALIA",
"MAMMALIA"), Order = c("CARNIVORA", "CARNIVORA", "CARNIVORA",
"CARNIVORA", "CARNIVORA", "CARNIVORA", "CARNIVORA", "CARNIVORA",
"CARNIVORA", "CARNIVORA", "CARNIVORA", "CARNIVORA", "CARNIVORA",
"CARNIVORA", "CARNIVORA", "CARNIVORA", "CARNIVORA", "CARNIVORA",
"CARNIVORA", "CARNIVORA"), Family = c("HERPESTIDAE", "HERPESTIDAE",
"HERPESTIDAE", "HERPESTIDAE", "HERPESTIDAE", "HERPESTIDAE",
"HERPESTIDAE", "HERPESTIDAE", "HERPESTIDAE", "HERPESTIDAE",
"HERPESTIDAE", "HERPESTIDAE", "HERPESTIDAE", "HERPESTIDAE",
"HERPESTIDAE", "HERPESTIDAE", "HERPESTIDAE", "HERPESTIDAE",
"HERPESTIDAE", "HERPESTIDAE"), Sampling.Unit.Name = c("CT-UDZ-3-06",
"CT-UDZ-3-01", "CT-UDZ-3-06", "CT-UDZ-1-01", "CT-UDZ-1-01",
"CT-UDZ-3-01", "CT-UDZ-3-06", "CT-UDZ-1-01", "CT-UDZ-3-01",
"CT-UDZ-1-20", "CT-UDZ-1-07", "CT-UDZ-1-09", "CT-UDZ-1-01",
"CT-UDZ-1-09", "CT-UDZ-1-02", "CT-UDZ-1-07", "CT-UDZ-1-07",
"CT-UDZ-1-01", "CT-UDZ-1-04", "CT-UDZ-1-07"), Latitude = c(-7.73367,
-7.74632, -7.73367, -7.84697, -7.84697, -7.74632, -7.73367,
-7.84697, -7.74632, -7.79673, -7.83554, -7.8222, -7.84697,
-7.8222, -7.84762, -7.83554, -7.83554, -7.84697, -7.83463,
-7.83554), Longitude = c(36.87904, 36.89205, 36.87904, 36.82797,
36.82797, 36.89205, 36.87904, 36.82797, 36.89205, 36.8917,
36.86666, 36.84057, 36.82797, 36.84057, 36.86584, 36.86666,
36.86666, 36.82797, 36.82775, 36.86666), Project.Name = c("Udzungwa",
"Udzungwa", "Udzungwa", "Udzungwa", "Udzungwa", "Udzungwa",
"Udzungwa", "Udzungwa", "Udzungwa", "Udzungwa", "Udzungwa",
"Udzungwa", "Udzungwa", "Udzungwa", "Udzungwa", "Udzungwa",
"Udzungwa", "Udzungwa", "Udzungwa", "Udzungwa"), Sampling.Event = c(2009.01,
2009.01, 2009.01, 2009.01, 2009.01, 2009.01, 2009.01, 2009.01,
2009.01, 2009.01, 2009.01, 2009.01, 2009.01, 2009.01, 2009.01,
2009.01, 2009.01, 2009.01, 2009.01, 2009.01), Photo.Date = c("07.11.09",
"02.11.09", "07.11.09", "01.08.09", "01.08.09", "02.11.09",
"07.11.09", "01.08.09", "02.11.09", "06.08.09", "06.08.09",
"29.07.09", "04.08.09", "12.08.09", "07.08.09", "06.08.09",
"06.08.09", "11.08.09", "14.08.09", "06.08.09"), Photo.Time = structure(c(68187,
16168, 68192, 19040, 19038, 16170, 68190, 19039, 16171, 77446,
8870, 3187, 504, 5719, 18815, 8866, 8868, 7215, 69942, 8871
), class = c("hms", "difftime"), units = "secs"), Number.of.Animals = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
Camera.Start.Date.and.Time = c("31.10.09 13:50", "28.10.09 15:21",
"31.10.09 13:50", "27.07.09 13:16", "27.07.09 13:16", "28.10.09 15:21",
"31.10.09 13:50", "27.07.09 13:16", "28.10.09 15:21", "30.07.09 11:58",
"25.07.09 13:02", "28.07.09 10:32", "27.07.09 13:16", "28.07.09 10:32",
"25.07.09 15:52", "25.07.09 13:02", "25.07.09 13:02", "27.07.09 13:16",
"24.07.09 09:36", "25.07.09 13:02"), Camera.End.Date.and.Time = c("30.11.09 11:45",
"04.12.09 12:35", "30.11.09 11:45", "28.08.09 15:39", "28.08.09 15:39",
"04.12.09 12:35", "30.11.09 11:45", "28.08.09 15:39", "04.12.09 12:35",
"06.08.09 23:59", "28.08.09 11:24", "31.08.09 09:34", "28.08.09 15:39",
"31.08.09 09:34", "28.08.09 13:43", "28.08.09 11:24", "28.08.09 11:24",
"28.08.09 15:39", "30.08.09 10:07", "28.08.09 11:24"), camera_days = c("",
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "")), row.names = c(492L, 503L, 507L, 521L, 524L,
528L, 534L, 536L, 542L, 575L, 592L, 609L, 611L, 618L, 647L, 650L,
651L, 657L, 665L, 666L), class = "data.frame")
yr2009$Camera.End.Date.and.Time <- dmy_hm(yr2009$Camera.End.Date.and.Time)
yr2009$Camera.Start.Date.and.Time <- dmy_hm(yr2009$Camera.Start.Date.and.Time)
cam.days <- function(dtaframe, year) {
yr <- dtaframe[dtaframe$Sampling.Event == year, ]
yr$ndays <- as.numeric(difftime(yr$Camera.End.Date.and.Time, yr$Camera.Start.Date.and.Time))
selvar <- subset(yr, select = c(Sampling.Unit.Name, Camera.Start.Date.and.Time, Camera.End.Date.and.Time, ndays))
cam.days <- unique(selvar)
}
##new column
# camera trap days
camera_days<-cam.days(yr2009,2009.01)
summary(camera_days[,2:4])
#> Camera.Start.Date.and.Time Camera.End.Date.and.Time ndays
#> Min. :2009-07-24 09:36:00.0 Min. :2009-08-06 23:59:00 Min. : 7.501
#> 1st Qu.:2009-07-25 15:09:30.0 1st Qu.:2009-08-28 13:08:15 1st Qu.:31.553
#> Median :2009-07-27 23:54:00.0 Median :2009-08-29 12:53:00 Median :33.921
#> Mean :2009-08-19 18:55:52.5 Mean :2009-09-19 10:35:45 Mean :30.653
#> 3rd Qu.:2009-08-22 00:48:45.0 3rd Qu.:2009-09-23 04:06:45 3rd Qu.:34.691
#> Max. :2009-10-31 13:50:00.0 Max. :2009-12-04 12:35:00 Max. :37.022
Created on 2023-07-18 with reprex v2.0.2