How to remove duplicated (by name) column in data.tables in R?
.SDcols
approaches would return a copy of the columns you're selecting. Instead just remove those duplicated columns using :=
, by reference.
dt[, which(duplicated(names(dt))) := NULL]
# x
# 1: 1
How about
dt[, .SD, .SDcols = unique(names(dt))]
This selects the first occurrence of each name (I'm not sure how you want to handle this).
As @DavidArenburg suggests in comments above, you could use check.names=TRUE
in data.table()
or fread()