镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

dcast naming: 1 vs. 2+ value.var #6582

Open

Description

@sluga

Suppose I have a datatable with columns id, part, etc. & want to widen it with id ~ part.

  1. If there is a single value.var column, the input & output look like this:
      id  part   foo
   <num> <num> <int>
1:     1     1     1
2:     1     2     2
3:     2     1     3
      id     1     2
   <num> <int> <int>
1:     1     1     2
2:     2     3    NA
  1. If there are multiple value.var columns, the input & output look like this:
      id  part   foo   bar
   <num> <num> <int> <int>
1:     1     1     1     4
2:     1     2     2     5
3:     2     1     3     6
      id foo_1 foo_2 bar_1 bar_2
   <num> <int> <int> <int> <int>
1:     1     1     2     4     5
2:     2     3    NA     6    NA

For interactive use this is fine, but when I'm using this code in an automated pipeline, I find myself wishing for a consistent treatment of column names, i.e. that the first result would be:

      id foo_1 foo_2
   <num> <int> <int>
1:     1     1     2 
2:     2     3    NA

Could we switch to this treatment or provide an argument for doing so?


library(data.table) # 1.16.2
dt1 <- data.table(id = c(1, 1, 2), part = c(1, 2, 1), foo = 1:3)
dcast(dt1, id ~ part, value.var = 'foo')
dt1[, bar := 4:6]
dcast(dt1, id ~ part, value.var = c('foo', 'bar'))

Activity

  1. changed the title [-]dcast naming [/-] [+]dcast naming: 1 vs. 2+ value.var[/+] on Oct 19, 2024
  2. tdhock commented on Oct 23, 2024

    @tdhock
    Member

    good question, I always thought this behavior was funny / annoying sometimes too.
    tldr; I think it would be difficult to support.

    • it would be a breaking change.
    • if added as an option, that would be difficult to maintain two separate code paths / test suites.

    also there could be a third part of the dcast names which is added when there is more than one fun.aggregate,

    > dcast(dt1, id ~ part, list(mean, sd), value.var = c('foo', 'bar'))
    Key: <id>
          id foo_mean_1 foo_mean_2 bar_mean_1 bar_mean_2 foo_sd_1 foo_sd_2 bar_sd_1
       <num>      <num>      <num>      <num>      <num>    <num>    <num>    <num>
    1:     1          1          2          4          5       NA       NA       NA
    2:     2          3        NaN          6        NaN       NA       NA       NA
       bar_sd_2
          <num>
    1:       NA
    2:       NA

    I think these differences in names may be annoying in some cases, but I also suspect that always outputting full three /two part names could be annoying in some cases too.

  3. tdhock commented on Oct 23, 2024

    @tdhock
    Member

    here is another funny inconsistency involving dcast output names

    > dcast(dt1, id ~ ., list(mean, sd), value.var="foo")
    Key: <id>
          id foo_mean    foo_sd
       <num>    <num>     <num>
    1:     1      1.5 0.7071068
    2:     2      3.0        NA
    > dcast(dt1, id ~ ., mean, value.var="foo")
    Key: <id>
          id     .
       <num> <num>
    1:     1   1.5
    2:     2   3.0
  4. tdhock commented on Jun 8, 2025

    @tdhock
    Member

    duplicate of #2364

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

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions