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
setDT(DT)[, fwrite(.SD, paste0("user", unique(userid),".csv")), by = userid, .SDcols=names(DT) ]
was struck by .SDcols=names(DT), IINM it's to force the .BY columns to be included in the output. This of course makes this unavailable on chained data.tables; the following would look relatively straightforward:
setDT(DT)[, fwrite(c(.BY, .SD), paste0("user", .BY$userid, ".csv")), by = userid]
(this works as DT[ , c(.BY, .SD), by = userid])
But fails currently because fwrite won't recycle the singletons.
The workaround is to do the recycling manually:
DT[ , by = userid, {
out = c(.BY, .SD)
# or for (i in seq_along(.BY)) {...}
out[[1L]] = rep(out[[1L]], .N)
fwrite(out, ...)
}]
Agree that would nicely simplifies the use case from twitter, but on the other hand might introduce confusion. I would rather change unique(userid) into userid[1L] and leave it as is.
IINM it's to force the .BY columns to be included in the output. This of course makes this unavailable on chained data.tables
Solution proposed by you is not generic enough to address chaining gap here, it will address this case only.
The case above c(.BY, .SD) would be addressed by #661 (if having .BY as data.table not only when i is a list).
We either should stay as is now, incorporate your suggestion, or provide a good replacement for c to handle recycling. Which naturally should be cbind.
Activity
jangorecki commentedon Apr 9, 2020
In short, you are asking for a list method for fwrite function?
MichaelChirico commentedon Apr 10, 2020
fwritealready works on lists:what doesn't work is:
jangorecki commentedon Apr 10, 2020
Agree that would nicely simplifies the use case from twitter, but on the other hand might introduce confusion. I would rather change
unique(userid)intouserid[1L]and leave it as is.Solution proposed by you is not generic enough to address chaining gap here, it will address this case only.
The case above
c(.BY, .SD)would be addressed by #661 (if having.BYas data.table not only wheniis a list).We either should stay as is now, incorporate your suggestion, or provide a good replacement for
cto handle recycling. Which naturally should becbind.MichaelChirico commentedon Apr 10, 2020
not sure I follow why, but anyway, agree
cbind+.BY-as-data.tablewould be an equally nice solution.