Skip to content

Inconsistent deep/shallow copying when subsetting using an always TRUE condition #4740

Open

Description

@NoProblemJack

Hi, I have noticed inconsistent behaviour when subsetting a data.table such that no rows are actually filtered.

dtb = data.table(a = c(1,3))

dtb1 = dtb[a %in% c(1,3)]
dtb2 = dtb[a != 4]
dtb3 = dtb[1==1]
dtb4 = dtb[!a %in% 4]

dtb
#>   a
#>  1: 1
#>  2: 3

dtb1
#>   a
#>  1: 1
#>  2: 3

dtb2
#>   a
#>  1: 1
#>  2: 3

dtb3
#>   a
#>  1: 1
#>  2: 3

dtb4
#>   a
#>  1: 1
#>  2: 3

address(dtb[[1]])
#> [1] "00000188a51c9410"
address(dtb1[[1]])
#> [1] "00000188a51f0810"
address(dtb2[[1]])
#> [1] "00000188a51f01d0"
address(dtb3[[1]])
#> [1] "00000188a51c9410"
address(dtb4[[1]])
#> [1] "00000188a51c9410"

dtb[, a := 999L]

dtb
#>      a
#> 1: 999
#> 2: 999

dtb1
#>    a
#> 1: 1
#> 2: 3

dtb2
#>    a
#> 1: 1
#> 2: 3

dtb3
#>      a
#> 1: 999
#> 2: 999

dtb4
#>      a
#> 1: 999
#> 2: 999



sessionInfo()
#> R version 4.0.2 (2020-06-22)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19041)
#> 
#> Matrix products: default
#> 
#> locale:
#> [1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252    LC_MONETARY=English_United #> Kingdom.1252
#> [4] LC_NUMERIC=C                            LC_TIME=English_United Kingdom.1252    
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] data.table_1.13.0
#> 
#> loaded via a namespace (and not attached):
#> [1] compiler_4.0.2 tools_4.0.2    tinytex_0.26   xfun_0.17     

For consistency, I would assume that a deep copy should be made no matter the form of the 'i' expression and whether any rows happen to be filtered by the subset condition. In my particular case, I encountered a bug because I went on to do in place manipulations on the new data.table, unknowingly altering the original.

This comment suggests to me that a deep copy is always to be expected.

#3214 (comment)

Thanks

Activity

  1. jangorecki commented on Oct 6, 2020

    @jangorecki
    Member

    Agree, thank you for reporting. Similar report was already made by @renkun-ken about DT[TRUE]. Your one lists more examples.

  2. renkun-ken commented on Oct 6, 2020

    @renkun-ken
    Member

    Similar with #2254.

  3. added this to the 2.0.0 milestone on Jan 7, 2024
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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions