Assign multiple results from function when grouping

If you make your function return a list you only need to call

dt[, myRegr(x, y), by = a]
#   a minX minY          k         m        r2
#1: 0   12    1 -0.3095238  8.285714 0.3176692
#2: 1   31    2 -1.0000000 37.000000 0.2500000

With

myRegr = function(x, y) {
  regr = lm.fit(cbind(1, x), y)
  coefs = regr$coef
  k = coefs[[2]]
  m = coefs[[1]]
  r2 = 1 - var(regr$residuals) / var(y)

  return (list(# minX = min(x),
               # minY = min(y),
               k = k,
               m = m,
               r2 = r2))
}

update

You might subset for x and y values and then join with the result of your function

result <- dt[dt[, .I[which.min(time)], by = a]$V1, .(a, x, y)]
result <- result[dt[, myRegr(x, y), by = a], on = .(a)]
result
#   a  x y          k         m        r2
#1: 0 12 3 -0.3095238  8.285714 0.3176692
#2: 1 34 4 -1.0000000 37.000000 0.2500000

You can modify your function to return a vector and dcast final result:

library(data.table)
myRegr = function(x, y) {
  regr <- lm.fit(cbind(1, x), y)
  c(
    regr$coef[[1]],
    regr$coef[[2]],
    1 - var(regr$residuals) / var(y)
  )
}
result <- df[, .(minX = min(x), minY = min(y), myRegr(x, y), c("m", "k", "r2")), a]
dcast(result, a + minX + minY ~ V4, value.var = "V3")

This solution is not perfect as I have to create V4 (add c("m", "k", "r2") vector). There should be a better way to do this (perhaps even not to use dcast). Maybe more experienced data.table users could advice on this?


Data:

df <- data.table(
  a = c(0, 0, 0, 1, 1, 1), 
  x = c(12, 21, 15, 34, 32, 31), 
  y = c(3, 1, 6, 4, 2, 8)
)

Tags:

R

Data.Table