John,

The issue is the `as.list(inJan[,"Days"])` in the call to `aggregate()`. This 
makes a list with 50 elements. Try `list(inJan[,"Days"])` instead. Note that 
there is still an error, but now coming from the `timediff()` function and 
unrelated to `aggregate()`.

Hope this helps.

Steve

On Saturday, February 28th, 2026 at 12:08 AM, Sorkin, John 
<[email protected]> wrote:

> 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.
>

______________________________________________
[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.

Reply via email to