Skip to content

Commit e54bd71

Browse files
author
Toby Dylan Hocking
committed
document select arg
1 parent 2a0549f commit e54bd71

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

vignettes/datatable-fread-and-fwrite.Rmd

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,14 +270,23 @@ if (requireNamespace("bit64", quietly = TRUE)) {
270270

271271
### 2.4 Column Order and Subset Control
272272

273-
To control the order and subset of columns written to file, subset the data.table before calling `fwrite()`. The `col.names` argument in `fwrite()` is a logical (TRUE/FALSE) that controls whether the header row is written, not which columns are written.
273+
To control the order and subset of columns written to file, you can use `[.data.table` to make a new table before calling `fwrite()`, but it is more efficient to use the `select` argument, which avoids making a copy.
274274

275275
```{r}
276276
dt = data.table(A = 1:3, B = 4:6, C = 7:9)
277-
278277
# Write only columns C and A, in that order
279278
fwrite(dt[, .(C, A)], "out.csv")
280279
cat(readLines("out.csv"), sep = "\n")
280+
fwrite(dt, "out.csv", select=c("C","A"))
281+
cat(readLines("out.csv"), sep = "\n")
282+
file.remove("out.csv")
283+
```
284+
285+
The `col.names` argument in `fwrite()` is a logical (TRUE/FALSE) that controls whether the header row is written, not which columns are written.
286+
287+
```{r}
288+
fwrite(dt, "out.csv", col.names=FALSE)
289+
cat(readLines("out.csv"), sep = "\n")
281290
file.remove("out.csv")
282291
```
283292

@@ -292,4 +301,4 @@ For users interested in detailed, up-to-date performance comparisons, we recomme
292301

293302
These benchmarks consistently show that `fread` and `fwrite` are highly competitive and often state-of-the-art for performance in the R ecosystem.
294303

295-
***
304+
***

0 commit comments

Comments
 (0)