Replacement error in numeric function

Hello everyone,

I am quite a newbie to R but I need it to analyse my camera trap data for my master thesis. I have found and downloaded a R script to construct species accumulation curves. With the script there came a couple of manual functions. I am trying to run a code executing the respective function. Its purpose is to return the time interval between two detections by calculating it from the start and end date of a camera trap station and add it to a new column.
However I receive the error:

Error in $<-.data.frame(*tmp*, "ndays", value = numeric(0)) :
replacement has 0 rows, data has 10543.

Now i have already found the exact problem and solution to it described here but it doesn't work for me. It was said all that is needed is to first create the respective column, which I did. I still get the same error.
Maybe something about the function is not correct but I am just running the approved script that I downloaded.
This is it:

cam.days <- function(dtaframe, year) {
** yr <- dtaframe[dtaframe$Sampling.Event == year, ]**
** yr$ndays <- as.numeric(difftime(yr$End.Date, yr$Start.Date))**
** selvar <- subset(yr, select = c(Sampling.Unit.Name, Start.Date, End.Date, ndays))**
** cam.days <- unique(selvar)**
}

##new column
yr2009$camera_days <-""

# camera trap days
camera_days<-cam.days(yr2009,2009.01)
summary(camera_days[,2:4])
write.table(camera_days, file="camera_days_2009.txt",quote=F, sep="\t",row.names = F)

yr2009 is the data frame where the new column should appear, 2009.01 is the Sampling.Event

Any ideas what could be wrong?

Many thanks,
Sonja

Please post the output of

dput(head(yr2009, 20))

Put a line with three back ticks before and after the output, like this:
```
pasted output of dput() goes here
```

The output is:

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")```

Hi Sonja, welcome to the forum.
It looks like your immediate problem is that

camera_days<-cam.days(yr2009,2009.01)

is asking for a column of data named 2009.09 which does not exist in your data.frame. In fact, it cannot exist as R will not allow a column name (variable) to be all numeric.

Is it possible you wanted to subset the data.frame? If so you will need to subset before running the function.

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

Hi,
thank you so much for your effort, this is already amazing!
I just don't understand why I get an output of only 58 observations because my original data frame has 10.543 and I want to get the number of days each camera was running (time difference for every row in the data frame yr2009).

It is hard to tell what the problem is without having your data set. I suggest you run each step of your function individually and look at the intermediate results yr and selvar. Are they what you expect?

dtaframe <- yr2009
year = 2009.01
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)

Okay thank you for this suggestion, I understand the code better now and I believe the output is just correct. Sorry for bothering, R is still a bit cryptic to me.

Thank you :smiling_face:
I didn't actually understand what was being done with this because it is using the custom function cam.days that I couldn't penetrate as R beginner. I just ran the code for the final output that I need on my own dataset later on. Eventually it worked, 2009.01 is not a variable but entries in the dataframe, but it is very good to know that a column name can't be all numeric!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.