New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
data.table not returning the correct splinefun by group #4298
Comments
I haven't looked at this issue deeply, but the workaround is just to add a The issue itself, I believe, is about the way how An tweaked-now-ok example based on yours# R version: 3.6.3 (2020-02-29)
library(data.table) # data.table_1.12.8
# mimic our data in simpler format
set.seed(1)
dt <- data.table(cat = rep(letters[1:3], each = 10),
x = 1:10)
dt[, y := x^0.5 * rnorm(.N, mean=runif(1, 1, 100), sd=runif(1, 1, 10)), by=cat]
# fit spline, segment the data by category
######## this works ########3
mod_splines <- dt[, .(Spline = list(splinefun(x=copy(x), y=copy(y), method = "natural"))),
by = cat]
# splinefun works such that you provide new values of x and it gives an output
# y from a spline fitted to y~x
# Can see they are all the same, which seems unlikely
mod_splines$Spline[[1]](5)
mod_splines$Spline[[2]](5)
mod_splines$Spline[[3]](5)
# alternative approach
alt_splines <- lapply(unique(dt$cat), function(x_cat){
splinefun(x=dt[cat==x_cat, ]$x,
y=dt[cat==x_cat, ]$y,
method = "natural")
})
# looks more realistic
alt_splines[[1]](5)
alt_splines[[2]](5)
alt_splines[[3]](5) # Matches the mod_splines one! |
The problem is happening in
|
@MichaelChirico If that's the case, it may be a bug? |
I think it's a bug for sure |
@jangorecki is it a dup of #507? |
Grouping is apparently overwriting the return value of other groups in the following example where producing a column with a list of functions:
If we do this in
base
, the functions are different:It appears the last group's function was copied to the other groups
The text was updated successfully, but these errors were encountered: