You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Suppose I have a datatable with columns id, part, etc. & want to widen it with id ~ part.
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
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'))
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.
Activity
[-]dcast naming [/-][+]dcast naming: 1 vs. 2+ value.var[/+]tdhock commentedon Oct 23, 2024
good question, I always thought this behavior was funny / annoying sometimes too.
tldr; I think it would be difficult to support.
also there could be a third part of the dcast names which is added when there is more than one
fun.aggregate,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.
tdhock commentedon Oct 23, 2024
here is another funny inconsistency involving dcast output names
tdhock commentedon Jun 8, 2025
duplicate of #2364