1

A function taking a data.table df as input uses the data.table := operation should return the modified data.table but returns nothing even with explicit return(df) statement. The function returns the data.table if we force a transformation to data.table with data.table(df).

What is the reason behind this behavior? And what are the good practices in coding functions with the data.table := operator?

Here is a minimal example:

library(data.table)

data <- data.table(x = 1:3)

test_function_1 <- function(df){
    df[, new_column := 1]
}

test_function_2 <- function(df){
    df[, new_column := 1]
    return(df)
}

test_function_3 <- function(df){
    df[, new_column := 1]
    data.table(df)
}

test_function_1(data) # returns nothing
test_function_2(data) # returns nothing
test_function_3(data) # returns the modified data.table

Here is my sessionInfo() if needed:

> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)


Matrix products: default

    [...]

attached base packages:
[1] stats     graphics  grDevices utils     datasets 
[6] methods   base     

other attached packages:
[1] data.table_1.11.4
elalaouifaris
  • 67
  • 1
  • 5
  • 1
    The `:=` operator basically changes the original data table and suppresses the print. Check `data`. You'll find that the new column has been added to it. Instead, I suggest something along the lines of : `df[,.(x,new_column=1)]`. This doesn't change the original dt and returns a table without any problems – Rohit Aug 30 '18 at 10:04

1 Answers1

3
library(data.table)

data <- data.table(x = 1:3)

test_function_1 <- function(df){
    df[, new_column := 1][]
}

test_function_2 <- function(df){
    df[, new_column := 1][]
    return(df)
}

test_function_3 <- function(df){
    df[, new_column := 1]
    data.table(df)
}

test_function_1(data) # returns the modified data.table
test_function_2(data) # returns the modified data.table
test_function_3(data) # returns the modified data.table

more info: H E R E

Andre Elrico
  • 10,956
  • 6
  • 50
  • 69