I have a large dataframe containing data from several participants. Each participant's data extends over many rows and I need to save the data for each participant in a separate text file. As suggested elsewhere (Split dataframe into multiple output files), I am using data.table::fwrite
for this job. However, this prompts a message and I am unsure about its interpretation in my case:
Empty data.table (0 rows) of 1 col: A
I have already searched for possible explanations, but felt that none of these directly addressed my problem (e.g. see: unclear error/message: "Empty data.table (0 rows) of 1 col:"; https://github.com/Rdatatable/data.table/issues/3262).
To illustrate, I have generated a sample script, which is similar in nature and replicates this message:
# generate data table
DT = data.table(A=c(1,2,1,2,1,2), B=c(1,2,1,1,2,2), C=c(1:6), D=c(20:15))
A B C D
1: 1 1 1 20
2: 2 2 2 19
3: 1 1 3 18
4: 2 1 4 17
5: 1 2 5 16
6: 2 2 6 15
# save data in separate files based on column A
DT[, fwrite(.SD, stringr::str_c("T", unique(A),".csv")), by=.(A)]
This generates the above message. Nonetheless, two files are created (as needed) and contain the relevant data.
fread("T1.csv")
B C D
1: 1 1 20
2: 1 3 18
3: 2 5 16
fread("T2.csv")
B C D
1: 2 2 19
2: 1 4 17
3: 2 6 15
The message prompt suggests to me, however, that something in my script didn't quite work (I don't intend to have, or save, empty data tables). Is something amiss here indeed or is it just the message prompt that's erroneous? Interestingly, when removing fwrite
, the message disappears, suggesting to me that the message prompt is linked to fwrite
:
DT[, .SD, by=.(A)]
1: 1 1 1 20
2: 1 1 3 18
3: 1 2 5 16
4: 2 2 2 19
5: 2 1 4 17
6: 2 2 6 15
Any advice (and explanations) would be much appreciated.