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
Copy file name to clipboardExpand all lines: vignettes/datatable-fread-and-fwrite.Rmd
+12-3Lines changed: 12 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -270,14 +270,23 @@ if (requireNamespace("bit64", quietly = TRUE)) {
270
270
271
271
### 2.4 Column Order and Subset Control
272
272
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.
274
274
275
275
```{r}
276
276
dt = data.table(A = 1:3, B = 4:6, C = 7:9)
277
-
278
277
# Write only columns C and A, in that order
279
278
fwrite(dt[, .(C, A)], "out.csv")
280
279
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")
281
290
file.remove("out.csv")
282
291
```
283
292
@@ -292,4 +301,4 @@ For users interested in detailed, up-to-date performance comparisons, we recomme
292
301
293
302
These benchmarks consistently show that `fread` and `fwrite` are highly competitive and often state-of-the-art for performance in the R ecosystem.
0 commit comments