[Rd] Potential improvements of ave? (Act 2)
SOEIRO Thomas
Thom@@@SOEIRO @end|ng |rom @p-hm@|r
Sat Apr 17 21:24:06 CEST 2021
Dear list,
This is a follow-up with another potential improvements of ave.
In the doc, x is documented as to be "a numeric", but this is not mandatory.
DF <- data.frame(x = letters, group = rep(1:2, each = 13))
ave(DF$x, DF$group, FUN = function(i) "a")
# [1] "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a"
# [20] "a" "a" "a" "a" "a" "a" "a"
However coercion can raise issues if the type of x and FUN(x) do not match. Coercion happens in split<-.default in the for loop with x[i] <- value[[j]].
(NB: In the following example, we can work around the problem by wrapping x with as.numeric.)
DF <- data.frame(x = Sys.Date() + 1:10, group = rep(1:2, each = 5))
ave(DF$x, DF$group, FUN = function(i) 1)
# Error in as.Date.numeric(value) : 'origin' must be supplied
So I have 2 questions/suggestions:
- Could the doc rather state that x must match the type of FUN(x) and warn for coercion?
- Could ave be more flexible (i.e. allow different type of x and FUN(x)) if using another approach than x[i] <- value[[j]] in split<-.default for recycling?
This has already been discussed on r-help and stackoverflow (e.g. https://stat.ethz.ch/pipermail/r-help/2016-November/442855.html)
Best,
Thomas
More information about the R-devel
mailing list