I ran into an issue with fread()
function from data.table
package. I know it is still experimental, but maybe I am making some mistake somewhere.
Here is reproducible example:
library(data.table)
test <- data.frame(a=rnorm(300), b=rnorm(300))
write.csv(test,"a.csv")
fread("a.csv")
Gives an error:
Error in rbindlist(allargs) :
Item 2 has 2 columns, inconsistent with item 1 which has 3 columns
And side question: Why should I ever leave row.names=TRUE
in write.csv
question? So far I had only problems with it because it adds one unnamed column to the data.
Thx.
As a workaround you can read the rownames as a new column, by setting header=FALSE
fread("a.csv",header=FALSE)
header' changed by user from 'auto' to FALSE
V1 V2 V3
1: a b
2: 1 -1.55640470495795 -1.344760319214
3: 2 2.89752713867643 2.48413035874463
4: 3 -0.493990961968582 0.119727513514055
5: 4 0.559770137546773 1.07420769675405
---
297: 296 0.585750601363698 -1.59845801200953
298: 297 -0.867339301988422 0.776738489388772
299: 298 0.0942821874550108 -0.649440075398178
300: 299 -0.308039637386426 -0.840171787291445
301: 300 0.358526722813896 -1.362322309472
From the help of fread
, it looks that all the examples are using row.names=FALSE
, so as you have mentioned this works fine:
write.csv(test,"b.csv",row.names=FALSE)
fread("b.csv")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With