Skip to content

Commit c2c5d89

Browse files
bypass name res for num select, avoid no-op shallow, and cols in place of select(keep x as list/dt)
1 parent 6047fa3 commit c2c5d89

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

R/fwrite.R

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,17 @@ fwrite = function(x, file="", append=FALSE, quote="auto",
4343
}
4444
# Handle select argument using .shallow()
4545
if (!is.null(select)) {
46-
cols = colnamesInt(x, select)
46+
if (is.integer(select) || is.numeric(select)) { # numeric/integer avoids O(#cols) name-match overhead
47+
cols = as.integer(select)
48+
} else {
49+
cols = colnamesInt(x, select)
50+
}
4751
if (is.data.table(x)) {
48-
x = .shallow(x, cols)
52+
if (length(cols) < NCOL(x) || !identical(cols, seq_len(NCOL(x)))) { # only build a shallow view when columns are reduced or reordered
53+
x = .shallow(x, cols)
54+
}
4955
} else {
50-
x = x[select]
56+
x = x[cols]
5157
}
5258
}
5359

0 commit comments

Comments
 (0)