Re: aggregate function does not work: arguments must have same length

Rui Barradas <[email protected]> Sat, 28 Feb 2026 08:44:32 +0000
Newsgroups gmane.comp.lang.r.general
Message-ID <[email protected]>
Às 08:40 de 28/02/2026, Rui Barradas escreveu:
> Às 05:08 de 28/02/2026, Sorkin, John escreveu:
>> PLEASE READ THIS ENTIRE MESSAGE. It explains my problem and contains a 
>> reproducible example of the problem.
>>
>> I am trying to run an aggregate function. The function produces the 
>> following error message:
>> ERROR MESSAGE:
>> Error in aggregate.data.frame(as.data.frame(x), ...) :
>>    arguments must have same length
>>
>> Everything beyond this line can be pasted into an R session and run:
>>
>> ####Start of code
>> #I am trying to write a code that will compute the number of hours for 
>> which we have data for each day.
>> #Here are my data:
>>
>>    inJan<-
>>      structure(list(NewTime = structure(c(1597363200, 1597366800,
>>       1597370400, 1597374000, 1597377600, 1597381200, 1597384800, 
>> 1597388400,
>>       1597392000, 1597395600, 1597399200, 1597402800, 1597406400, 
>> 1597410000,
>>       1597413600, 1597417200, 1597420800, 1597424400, 1597428000, 
>> 1597431600,
>>       1597435200, 1597438800, 1597442400, 1597446000, 1597449600, 
>> 1597453200,
>>       1597456800, 1597460400, 1597464000, 1597467600, 1597471200, 
>> 1597474800,
>>       1597478400, 1597482000, 1597485600, 1597489200, 1597492800, 
>> 1597496400,
>>       1597500000, 1597503600, 1597507200, 1597510800, 1597514400, 
>> 1597518000,
>>       1597521600, 1597525200, 1597528800, 1597532400, 1597536000, 
>> 1597539600
>>      ), class = c("POSIXct", "POSIXt"), tzone = "UTC"), Days = 
>> structure(c(18488,
>>       18488, 18488, 18488, 18488, 18488, 18488, 18488, 18488, 18488,
>>       18488, 18488, 18488, 18488, 18488, 18488, 18488, 18488, 18488,
>>       18488, 18488, 18488, 18488, 18488, 18489, 18489, 18489, 18489,
>>       18489, 18489, 18489, 18489, 18489, 18489, 18489, 18489, 18489,
>>       18489, 18489, 18489, 18489, 18489, 18489, 18489, 18489, 18489,
>>       18489, 18489, 18490, 18490), class = "Date")), class = 
>> "data.frame", row.names = c(NA,-50L))
>> inJan
>> names(inJan)
>>
>> #I have written a function which finds the number of hours between a 
>> last time last(x) and a first time first(x).
>>
>> # Number of follow-up hours in each day
>> timediff <- function(x) {
>> difftime(last(x),first(x),units="hours")
>>   }
>>
>> #The function I wrote is used in an aggregate function which
>> # finds the minimum and maximum time in each day, and then
>> # calls timediff to find the number of hours in each day.
>> zoop <- aggregate(inJan[,"NewTime"],as.list(inJan[,"Days"]),timediff)
>>
>> #When I run the aggregate function I receive and error:
>> #Error in aggregate.data.frame(as.data.frame(x), ...) :
>> #arguments must have same length
>>
>> #HOWEVER I believe inJan[,"NewTime"] and inJan[,"Days"] have the same 
>> length
>>
>> llength(inJan[,"NewTime"])
>> #[1] 50
>> length(inJan[,"Days"])
>> #[1] 50
>>
>> #Can someone tell my why the aggregate function thinks the arguments 
>> are a different length?
>> #### End of code
>>
>> Thank you,
>> John
>>
>> John David Sorkin M.D., Ph.D.
>> Professor of Medicine, University of Maryland School of Medicine;
>> Associate Director for Biostatistics and Informatics, Baltimore VA 
>> Medical Center Geriatrics Research, Education, and Clinical Center;
>> Former PI Biostatistics and Informatics Core, University of Maryland 
>> School of Medicine Claude D. Pepper Older Americans Independence Center;
>> Senior Statistician University of Maryland Center for Vascular Research;
>>
>> Division of Gerontology, Geriatrics and Palliative Medicine,
>> 10 North Greene Street
>> GRECC (BT/18/GR)
>> Baltimore, MD 21201-1524
>> Cell phone 443-418-5382
>>
>>
>>
>> ______________________________________________
>> [email protected] mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide https://www.R-project.org/posting- 
>> guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> Hello,
> 
> The problem is not in the input vectors' lengths, it's the output length.
> 
> When aggregating, I find it better to use the formula interface.
> It avoids the list() call and eventual errors due to as.list() being 
> mistaken for it.
> 
> Also, when using non base packages, please include a call to library() 
> or similar. I had to revise the function for it to work.
> 
> 
> # Number of follow-up hours in each day
> timediff <- function(x) {
>    difftime(dplyr::last(x), dplyr::first(x), units = "hours")
> }
> 
> aggregate(NewTime ~ Days, inJan, timediff)
> #>         Days  NewTime
> #> 1 2020-08-14 23 hours
> #> 2 2020-08-15 23 hours
> #> 3 2020-08-16  1 hours
> 
> 
> Alternative versions:
> 
> # This is what Peter Daalgard proposed (list, not as.list)
> aggregate(inJan[,"NewTime"], list(inJan[,"Days"]), timediff)
> 
> # another one
> aggregate(inJan[["NewTime"]], list(inJan[["Days"]]), timediff)
> 
> 
> # not relevant, but if you ever want just a vector, here it is.
> tapply(inJan[,"NewTime"], inJan[,"Days"], timediff)
> #> 2020-08-14 2020-08-15 2020-08-16
> #>         23         23          1
> 
> 
> Hope this helps,
> 
> Rui Barradas
> 
> ______________________________________________
> [email protected] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide https://www.R-project.org/posting- 
> guide.html
> and provide commented, minimal, self-contained, reproducible code.

Sorry, it's Peter Dalgaard.
:(

Rui Barradas