Skip to content

Using variables with names "i" and "j" with shift fails in v1.14.7 #5547

@neovom

Description

@neovom

# Summary

If we use variables with names "i" or "j" in shift with data.table v1.14.7 we get an error.

Last workable version: v1.14.6

# Minimal reproducible example

library(data.table)                                 
print(sessionInfo())                                
options(datatable.verbose = TRUE)                   
                                                    
# using i in shift fails                            
X <- data.table(x = 1:4, g = as.factor(c(1,1,2,2))) 
i <- 1                                              
X[, y := shift(x, n = abs(i), type = "lag"), .(g)]  
X[]                                                 

#Argument 'by' after substitute: .(g)                                                                       
#Detected that j uses these columns: [x]                                                                    
#Finding groups using forderv ... forder.c received 4 rows and 1 columns                                    
#0.001s elapsed (0.000s cpu)                                                                                
#Finding group sizes from the positions (can be avoided to save RAM) ... 0.000s elapsed (0.000s cpu)        
#lapply optimization is on, j unchanged as 'shift(x, n = abs(i), type = "lag")'                             
#GForce optimized j to 'gshift(x, n = abs(i), type = "lag")'                                                
#Making each group and running j (GForce TRUE) ... gforce initial population of grp took 0.000              
#gforce assign high and low took 0.000                                                                      
#Error in abs(i) : non-numeric argument to mathematical function                                            
                                                    
# using j in shift fails                            
X <- data.table(x = 1:4, g = as.factor(c(1,1,2,2))) 
j <- 1                                              
X[, y := shift(x, n = abs(j), type = "lag"), .(g)]  
X[]       

#Argument 'by' after substitute: .(g)                                                                                                                           
#Detected that j uses these columns: [x]                                                                                                                        
#Finding groups using forderv ... forder.c received 4 rows and 1 columns                                                                                        
#0.000s elapsed (0.001s cpu)                                                                                                                                    
#Finding group sizes from the positions (can be avoided to save RAM) ... 0.000s elapsed (0.000s cpu)                                                            
#lapply optimization is on, j unchanged as 'shift(x, n = abs(j), type = "lag")'                                                                                 
#GForce optimized j to 'gshift(x, n = abs(j), type = "lag")'                                                                                                    
#Making each group and running j (GForce TRUE) ... gforce initial population of grp took 0.000                                                                  
#gforce assign high and low took 0.000                                                                                                                          
#Error: Check that is.data.table(DT) == TRUE. Otherwise, :=, `:=`(...) and let(...) are defined for use in j, once only and in particular ways. #See help(":="). 
                                          
# using k in shift works                            
X <- data.table(x = 1:4, g = as.factor(c(1,1,2,2))) 
k <- 1                                              
X[, y := shift(x, n = abs(k), type = "lag"), .(g)]  
X[]         
                                        
#Argument 'by' after substitute: .(g)                                                               
#Detected that j uses these columns: [x]                                                            
#Finding groups using forderv ... forder.c received 4 rows and 1 columns                            
#0.000s elapsed (0.000s cpu)                                                                        
#Finding group sizes from the positions (can be avoided to save RAM) ... 0.000s elapsed (0.000s cpu)
#lapply optimization is on, j unchanged as 'shift(x, n = abs(k), type = "lag")'                     
#GForce optimized j to 'gshift(x, n = abs(k), type = "lag")'                                        
#Making each group and running j (GForce TRUE) ... gforce initial population of grp took 0.000      
#gforce assign high and low took 0.000                                                              
#gforce eval took 0.000                                                                             
#0.000s elapsed (0.000s cpu)                                                                        
#Assigning to 4 row subset of 4 rows                                                                
#RHS_list_of_columns == false                                                                       
#>        x      g     y  
#   <int> <fctr> <int>    
#1:     1      1    NA    
#2:     2      1     1    
#3:     3      2    NA    
#4:     4      2     3    
#>                        

# Output of sessionInfo()

R version 4.1.2 (2021-11-01)                                                  
Platform: x86_64-pc-linux-gnu (64-bit)                                        
Running under: CentOS Linux 7 (Core)                                          

Matrix products: default                                                      
BLAS:   /usr/R/4.1.2/lib64/R/lib/libRblas.so                            
LAPACK: /usr/R/4.1.2/lib64/R/lib/libRlapack.so                          

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

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

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

loaded via a namespace (and not attached):                                    
[1] compiler_4.1.2                                                            

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions