Skip to content

export shallow copy #2323

Description

@franknarf1

I'm posting in response to Matt's SO post and comments below.

I have a function, toopt(th, dat). The goal is to minimize the function in terms of its first argument. The second argument is a data.table. I guess this is a pretty common use case.

library(data.table)

f = function(dat, th){
    dat[, prob := dpois(x, th) ][]
}

toopt = function(th, dat){
    mydat = data.table:::shallow(dat)
    f(mydat, th)
    mydat[, - sum(log(prob))]
}

nid = 10
nobs = 10
par = 20

DT = data.table(id = seq_len(nid))[, .(x = rpois(nobs, par)), by=id]
optimize(toopt, dat = DT, c(0,100))
mean(DT$x) # the closed-form solution

Inside optim or optimize, the function is called many times as it searches for the optimum.

Sorry if this example is too trivial or abstract. In my actual example, I have a sequence of functions like f that add columns (stored in separate functions so I can isolate them for easier testing and to reduce duplication, since I have multiple toopt functions that are similar but not identical); and my DT has some columns that I don't need for the calculations in toopt.

Besides functions passed to optim, I also use / want to use shallow in other functions, so I can benefit from the efficiency of not making a copy without worrying about whether my input will be messed with (if I didn't use copy) or about whether I need to to prune my input table's columns down to the minimum required for the function (if I did use copy).

Activity

  1. franknarf1 commented on Jul 26, 2019

    @franknarf1
    ContributorAuthor

    Besides dtplyr #3665, another recent example from SO:

    So you want to create a new column in a data.table? Any reason this doesn't work: mtcarsDT[, newcol := myFunc()]? – Andrew 18 hours ago
    Yes and No. I don't really want to create a column, I just want to view the result of the expression next to the existing columns without having to save the column and then delete it later – Dean MacGregor 18 hours ago

  2. changed the title [-][Request] export shallow or similar[/-] [+]export shallow copy[/+] on Jul 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions