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
{{ message }}
Repository navigation
data.table(vector, matrix) can return duplicate column names #3193
Not ideal to be creating data.tables with duplicate names from the get-go... those names should be V2_1 and V2_2 i guess.
Fixes with check.names but I'm not sure creating duplicates like this by default is the right behavior; my only hesitation is I'm sure there are other more complicated cases that are still creating duplicates even if we fix this.
The fix to this specific issue can be very simple, however, just add 7 characters to this line:
Sowewhat related, I have noticed several cases when colnames are duplicated.
These may be some specific cases, when working with toy datasets and colnames V1, V2, ...
Some examples below for information.
library(data.table)
## case 1: setDT duplicates V1 and V2
lst = list(V1=1, V2=1, 1, 1)
setDT(lst)[] #check.names=TRUE solves this
DT = data.table(V1 = c(1L,2L),
V2 = 1:9,
V3 = round(rnorm(3),2),
V4 = LETTERS[1:3])
## case 2: computation in j duplicates V1
DT[, sum(V3), by = "V1"]
## case 3 (linked to case 2): rollup duplicates the V1 colname and bugs
rollup(DT, j = sum(V3), by = c("V4", "V2")) # ok
rollup(DT, j = sum(V3), by = c("V4", "V1")) # error
# Error in groupingsets.data.table(x, by = by, sets = sets, .SDcols = .SDcols, :
# There exists duplicated column names in the results,
# ensure the column passed/evaluated in `j` and those in `by` are not overlapping.
# So specifying a different name works:
rollup(DT, j = .(SumV3 = sum(V3)), by = c("V4", "V1")) # ok
Activity
Atrebas commentedon Feb 3, 2019
Sowewhat related, I have noticed several cases when colnames are duplicated.
These may be some specific cases, when working with toy datasets and colnames V1, V2, ...
Some examples below for information.
jangorecki commentedon Feb 4, 2019
@Atrebas thanks for investigation. Groupings sets functions just wraps around ordinary grouping, thus are affected.
jangorecki commentedon Apr 5, 2020
Related to #4124
jangorecki commentedon Apr 7, 2020
@Atrebas
setDTis designed for speed, so the less the checks the better, any checks on quality of data/metadata should be on user sidejangorecki commentedon Apr 7, 2020
This issue is caused by a fact that data.table has check.names=FALSE by default, while data.frame has check.names=TRUE. I filled #4362
MichaelChirico commentedon Jul 11, 2025
I think this is fixed by #4363, though the breaking change means it's an "eventually" thing.
(textproc/R-data.table) Updated 1.16.4 to 1.18.4, Fix build against 4…
(textproc/R-data.table) Updated 1.16.4 to 1.18.4, Fix build against 4…