Search code examples
rdplyrdatediff

Problem with the combination of aggregate() and ave() with dates


I have some problem with the combination of aggregate() and ave() in the same function with dates (format 2020-03-16), if I make:

#Raw nest data
raw.Atta<- read.csv("https://raw.githubusercontent.com/Leprechault/trash/main/cres_ninho_ed.csv", sep=";", h=T)
str(raw.Atta)

## Create the days in each class
Atta.db.1 <- merge(raw.Atta, 
  within(
    aggregate(data ~ ninho + classe, raw.Atta, max),
    step3 <- ave(data, ninho, FUN = function(x) diff(c(0, x)))
  ),
  by = c("ninho", "classe"),
  all = TRUE
)

Error in r[i1] - r[-length(r):-(length(r) - lag + 1L)] :
    non-numeric argument to binary operator error

And I make some change the dates for another format:

raw.Atta <- raw.Atta %>%
  mutate(date = as.POSIXlt(data, format = "%Y-%m-%d")) # convert to datetime object

Error in model.frame.default(formula = data ~ ninho + classe, data = raw.Atta) :

creates a new problem!!

Please some ideas?

Thanks in advance!


Solution

  • Try this. The issue has its origin in diff() as it requires a date variable and yours is a character one:

    #Raw nest data
    raw.Atta<- read.csv("https://raw.githubusercontent.com/Leprechault/trash/main/cres_ninho_ed.csv", sep=";", h=T)
    str(raw.Atta)
    
    ## Create the days in each class
    Atta.db.1 <- merge(raw.Atta, 
                       within(
                         aggregate(data ~ ninho + classe, raw.Atta, max),
                         step3 <- ave(data, ninho, FUN = function(x) diff(c(0, as.Date(x))))
                       ),
                       by = c("ninho", "classe"),
                       all = TRUE
    )