X[Y] join synatx not adding columns when called inside a function after having substituted names of X

Open
#3,960 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
r
Domain
data

Research direction

Start by running the reproducible function f(X) and the X[Y, x := i.x, on = "b"] join shown in the issue, comparing behavior before and after names(X) is changed. Trace how the join and column assignment handle the renamed table, then add a regression test showing that x is retained in X after f(X) returns.

Written by the indexing model from the issue text.

Description

bug joins

Here a column 'x' is joined to the table X inside a function. However, when setting names(X) the way it's done below, the 'x' column is not actually added to X. Removing the name-change line, however, produces the desired output.

require(data.table)

# function that left-joins an 'x' column to X
f <- function(X){
    Y <- data.table(x = runif(3), b = letters[1:3])
    X[Y, x := i.x, on = "b"]
}

X <- data.table(a = runif(10), b = letters[1:10])

#------ changing names of X like this makes it so that 'x' disappears after f(X) has returned
names(X) <- gsub("a", "c", names(X))
#--------------------------------------------------------------------------------------------------------

f(X) 
print(X)

## 'x' column is missing:
#             c b
# 1: 0.07323440 a
# 2: 0.75673707 b
# 3: 0.01909958 c
# 4: 0.12090685 d
# 5: 0.45551750 e
# 6: 0.21110334 f
# 7: 0.81974756 g
# 8: 0.40734517 h
# 9: 0.24263204 i
# 10: 0.38344870 j
> sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.3 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=nb_NO.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=nb_NO.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=nb_NO.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=nb_NO.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] data.table_1.12.2

loaded via a namespace (and not attached):
 [1] MASS_7.3-51.1     zoo_1.8-6         compiler_3.6.1    magrittr_1.5      strucchange_1.5-1 sandwich_2.5-1    tools_3.6.1      
 [8] nlme_3.1-140      urca_1.3-0        grid_3.6.1        vars_1.5-3        lmtest_0.9-37     RKlav_0.2         lattice_0.20-38  
Dominant language
R
Stars
3.9k
Forks
1.1k
Avg merge
14h 4m
Merged PRs (30d)
4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rdatatable/data.table

All issues in Rdatatable/data.table

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.