Skip to content

Transformation/assignment through := lapply(.SD, ...) can switch column names #4874

Closed

Description

@joesnacks

When using := lapply(.SD, ...) to transform columns in place, if .SDcols contains duplicates and assignment is done by groups, then column names can be switched.

Example from SO:

dt = data.table(
  group = c(rep('group1',5),rep('group2',5)),
  x = as.numeric(c(1:5, 1:5)),
  y = as.numeric(c(5:1, 5:1)),
  z = as.numeric(c(1,2,3,2,1, 1,2,3,2,1))
)
cols = c('y','x','y','z') # contains a duplicate column name
dt[,(cols) := lapply(.SD,identity), .SDcols=cols] # Error (OK)
dt[,(cols) := lapply(.SD,identity), .SDcols=cols, by=group] # No error! Columns 'y' and 'z' are now switched

R version 3.6.0 (2019-04-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server x64 (build 14393)

Activity

  1. changed the title [-]Transformation/assignment through `:= lapply(.SD, ...)` can switch column names[/-] [+]Transformation/assignment through := lapply(.SD, ...) can switch column names[/+] on Jan 14, 2021
  2. ben-schwen commented on Jan 20, 2021

    @ben-schwen
    Member

    Hi,

    could you also add information about which version of data.table you are using? I can't reproduce it with 1.3.6 and Windows 10 or Debian.

  3. joesnacks commented on Jan 20, 2021

    @joesnacks
    Author

    Here's the full output of sessionInfo():

    R version 3.6.0 (2019-04-26)
    Platform: x86_64-w64-mingw32/x64 (64-bit)
    Running under: Windows Server x64 (build 14393)

    Matrix products: default

    locale:
    [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
    [4] LC_NUMERIC=C LC_TIME=English_United States.1252

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

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

    loaded via a namespace (and not attached):
    [1] compiler_3.6.0 tools_3.6.0

    ======================

    I have also been able to replicate it on another machine with a slightly different configuration:

    R version 3.5.2 (2018-12-20)
    Platform: x86_64-w64-mingw32/x64 (64-bit)
    Running under: Windows >= 8 x64 (build 9200)

    Matrix products: default

    locale:
    [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
    [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
    [5] LC_TIME=English_United States.1252

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

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

    loaded via a namespace (and not attached):
    [1] compiler_3.5.2 tools_3.5.2 yaml_2.2.0

  4. ben-schwen commented on Jan 20, 2021

    @ben-schwen
    Member

    It seems that this behavior was fixed with #3771. Hence, any version >= 1.12.4 should work.

  5. jangorecki commented on Jan 20, 2021

    @jangorecki
    Member

    Thanks @ben-schwen to taking care of this issue! @joesnacks could you please upgrade your package to ensure issue is resolved for you?

  6. joesnacks commented on Jan 20, 2021

    @joesnacks
    Author

    Thanks! I upgraded to version 1.12.8 and can confirm that the issue is fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions