0

Suppose, we have data.table with some columns:

DT <- data.table(c1 = c(1, 2, 3), c2 = c(1, 2, 3), ...)

and have function which returns data.frame:

fn_ret_df <- function(val1, val2) {
    return(data.frame(newC1 = val1^2, newC2 = val2/2))
}

How to concat initial DT and results of fn_ret_df(c1, c2) after applying for each line?

Here's what works for me now, but it has memory consumption problem:

DT[, cbind(fn_ret_df(c1, c2), c1, c2)]
  • You can't add the values using `data.table` methods? Is there a reason you're relying on another function to mutate `DT`? Working within `data.table` is usually the solution to a memory consumption problem. – De Novo Mar 19 '18 at 21:58
  • 1
    As hinted in above comment, you can do this directly like `DT[, c("newC1", "newC2") := .(val1^2, val2 / 2)]`. You're `cbind` method will require multiple copies which are avoided with `:=`. – lmo Mar 19 '18 at 22:15
  • @DanHall and @lmo: The function is in a package and has a complex logic. Thus, i can't use `:=` in simple way. – Pavel Lyubin Mar 20 '18 at 05:14
  • Many thanks. I found solution in related questions - [here](https://stackoverflow.com/a/11308946/4957055) – Pavel Lyubin Mar 20 '18 at 05:35

0 Answers0