2
votes

I am trying to write to disc a data.table with the following structure:

Classes ‘data.table’ and 'data.frame':  408776 obs. of  13 variables:
 $ date     : IDate, format: "2013-02-01" "2013-02-01" "2013-02-01" "2013-02-01" ...
 $ hour     : int  1 1 1 1 1 1 2 2 2 2 ...
 $ time     :Class 'ITime'  int [1:408776] 16 186 218 229 463 474 16 186 208 218 ...
 $ bids_med : num  NA NA NA 2.1 2.1 4.6 NA 7.5 7.5 7.5 ...
 $ bids_n   : int  NA NA NA 2 2 2 NA 4 4 4 ...
 $ asks_med : num  NA NA NA 21.5 21.5 21.5 NA 21 21 21 ...
 $ asks_n   : int  NA NA NA 2 2 2 NA 5 5 5 ...
 $ bprice   : num  5 4.2 4.2 4.2 5 5 9 9 9 10 ...
 $ bqty     : int  19 10 10 10 5 5 25 25 50 20 ...
 $ aprice   : num  16 16 21 21 21 21 19 19 19 19 ...
 $ aqty     : int  25 6 50 50 50 50 50 50 50 50 ...
 $ lastprice: num  7 16 16 16 16 16 9 9 9 9 ...
 $ lastqty  : int  31 19 6 6 6 6 25 25 25 25 ...
 - attr(*, "sorted")= chr  "date" "hour" "time"
 - attr(*, ".internal.selfref")= 

and I am getting the following error:

> write.csv("data/probit.csv", training, row.names = FALSE)
Error in charToDate(x) : 
  character string is not in a standard unambiguous format

To reproduce:

structure(list(date = structure(c(15737L, 15737L, 15737L, 15737L, 
15737L, 15737L), class = c("IDate", "Date")), hour = c(1L, 1L, 
1L, 1L, 1L, 1L), time = structure(c(16L, 186L, 218L, 229L, 463L, 
474L), class = "ITime"), bids_med = c(NA, NA, NA, 2.1, 2.1, 4.6
), bids_n = c(NA, NA, NA, 2L, 2L, 2L), asks_med = c(NA, NA, NA, 
21.5, 21.5, 21.5), asks_n = c(NA, NA, NA, 2L, 2L, 2L), bprice = c(5, 
4.2, 4.2, 4.2, 5, 5), bqty = c(19L, 10L, 10L, 10L, 5L, 5L), aprice = c(16, 
16, 21, 21, 21, 21), aqty = c(25L, 6L, 50L, 50L, 50L, 50L), lastprice = c(7, 
16, 16, 16, 16, 16), lastqty = c(31L, 19L, 6L, 6L, 6L, 6L)), .Names = c("date", 
"hour", "time", "bids_med", "bids_n", "asks_med", "asks_n", "bprice", 
"bqty", "aprice", "aqty", "lastprice", "lastqty"), sorted = c("date", 
"hour", "time"), class = c("data.table", "data.frame"), row.names = c(NA, 
-6L))

I am quite lost what to do with it. Any suggestions what might help? Thx.

1
A (more easily) reproducible example, like what you get from dput(head(DT)), might make it easier to answer. - Frank
Thanks, was thinking how to reproduce that and did not know about dput! Will remember. - krhlk
Downvoted the question because, as answer reveals, it is of type: "Please find a bug in my code for me, here is my code:". Additionally, 1. reader can learn very little by reading this Q&A (importance of checking code after self?); 2. Question title is misleading; therefore I propose to hide this thread from search by downvoting it. - Daniel Krizian
@DanielKrizian On the other hand, the title represents the problem as he saw it, and others down the road might misapprehend the situation in the same way (searching on such terms and then landing here thanks to the title). The "thread"/question will also show up in searches of write.csv with Error in charToDate(x), which is arguably useful. - Frank
@Frank, edits (to title) are among other reasons exactly for that: correcting the misapprehension down the road. Arguably, there will be users clicking the title link, landing here and reading in waste because they thought this thread was about IDate, ITime classes. - Daniel Krizian

1 Answers

6
votes

You just had the arguments to write.csv out of order. If passing by position, the data.frame or data.table comes first, then the file name. You can write file= first, too:

write.csv(file="out.csv",training,row.names=FALSE)

or

write.csv(training,"out.csv",row.names=FALSE)