Skip to content

Using setDF() within a function will not modify global object, while setDT() does? #4816

@MatthieuStigler

Description

@MatthieuStigler

Note: this was first asked on stack, but asking here as it seems highly technical?

I am converting back and forth from data.frame to data.table using a workflow like: setDT() + some data.table operation + setDF() (see this post). Using setDF() will correctly transform back my input into a data.frame.

However, when I use exactly the same code, but within a function, the input is not transformed back into an data.frame anymore after a setDF() call, while the output will still correctly be converted to a df. I guess this comes from am ambiguity at that point whether the object of setDF() refers to the local data within the function or the global data in the user workspace? Strangely, this does not happen to setDT()?

So how can I ensure that I can use the workflow setDT() + some data.table operation + setDF() within a function, and that setDF() modifies back the input to that function?

library(data.table)
data(iris)

## using setDF outside of function
setDT(iris)
iris[, Sepal.Length.Mean := mean(Sepal.Length), by = Species]
setDF(iris)

class(iris)
#> [1] "data.frame"

## using setDF within a function
fo <- function(df){
  setDT(df)
  df[, Sepal.Length.Mean := mean(Sepal.Length), by = Species]
  setDF(df)
  df
}

res <- fo(iris)
class(iris)
#> [1] "data.table" "data.frame"

Created on 2020-11-17 by the reprex package (v0.3.0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions