diff --git a/R/data.table.R b/R/data.table.R index 66fa75f50..751e2f3f3 100644 --- a/R/data.table.R +++ b/R/data.table.R @@ -2923,9 +2923,8 @@ setDT = function(x, keep.rownames=FALSE, key=NULL, check.names=FALSE) { } } - # setalloccol results in a shallow copy. Must be performed before class setting, - # to have the class apply only to the new copy. #4784 - setalloccol(x) + # setalloccol results in a shallow copy. Must be performed before class setting not to affect other copies of x (#4784) + x = .shallow(x) rn = if (!identical(keep.rownames, FALSE)) rownames(x) else NULL setattr(x, "row.names", .set_row_names(nrow(x))) @@ -2933,7 +2932,8 @@ setDT = function(x, keep.rownames=FALSE, key=NULL, check.names=FALSE) { # fix for #1078 and #1128, see .resetclass() for explanation. setattr(x, "class", .resetclass(x, 'data.frame')) - + setalloccol(x) + if (!is.null(rn)) { nm = c(if (is.character(keep.rownames)) keep.rownames[1L] else "rn", names(x)) x[, (nm[1L]) := rn]