I've joined two data tables and am calculating means based on a subset of that data. The code below runs properly when it's not within a function that I wrote, but I'm getting this error when I try to use the function:
Error in `[.data.table`(poll.name, AQ.Date >= Cdate & AQ.Date < Cdate + :
i evaluates to a logical vector length 159 but there are 2797432 rows. Recycling of logical i is no longer allowed as it hides more bugs than is worth the rare convenience. Explicitly use rep(...,length=.N) if you really need to recycle.
My function:
myfunc <- function(linked.dat, poll.name) {
linked.dat[,
`:=` (t1.avg = mean(poll.name[AQ.Date >= Cdate & AQ.Date < Cdate + 1], na.rm = TRUE),
t2.avg = mean(poll.name[AQ.Date >= Cdate + 1 & AQ.Date < Cdate + 2], na.rm = TRUE),
t3.avg = mean(poll.name[AQ.Date >= Cdate + 2 & AQ.Date <= Bdate], na.rm = TRUE),
total.avg = mean(poll.name)),
by = ID]
linked.pollname <- linked.dat
return(linked.pollname)
}
So using this function with the example df would look like:
myfunc(df, O3)
Some data:
df <- structure(list(O3 = c(21.1, 27.3, 23.8, 29.5, 23.8, 27.1, 31.6,
25.8, 31.2, 14, 19.1, 15.5, 15.6, 28.6, 16.9, 27.4, 30.1, 24.4,
21.2, 22.1, 26.1, 19.9), AQ.Date = structure(c(3679, 3681, 3682,
3683, 3680, 3685, 3686, 3687, 3684, 3689, 3673, 3675, 3677, 3678,
3686, 3687, 3688, 3692, 3681, 3693, 3695, 3696), class = "Date"),
ID = c("a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
"a", "a", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b"
), Cdate = structure(c(3673, 3673, 3673, 3673, 3673,
3673, 3673, 3673, 3673, 3673, 3673, 3673, 3677, 3677, 3677,
3677, 3677, 3677, 3677, 3677, 3677, 3677), class = "Date"),
Bdate = structure(c(3690, 3690, 3690, 3690, 3690, 3690,
3690, 3690, 3690, 3690, 3690, 3690, 3696, 3696, 3696, 3696,
3696, 3696, 3696, 3696, 3696, 3696), class = "Date"), Total_weeks = c(2.428571,
2.428571, 2.428571, 2.428571, 2.428571, 2.428571, 2.428571,
2.428571, 2.428571, 2.428571, 2.428571, 2.428571, 2.714286,
2.714286, 2.714286, 2.714286, 2.714286, 2.714286, 2.714286,
2.714286, 2.714286, 2.714286)), row.names = c(NA, -22L), class = "data.frame")
setDT(df)
I'm not understanding what this error means. What is the recycling referring to? Why is it only happening within the function? How can I adjust the function to address the error?