Skip to content

fread does not handle row.names produced by write.table #763

@biocyberman

Description

@biocyberman

Hi,
I am using data.table version 1.9.2
base::write.table has default row.names = TRUE. When using fread to this this the file, it doesn't handle the row names correctly. I understand that fread want to preserve the information from input file. Then it should consequently handle the header option correctly.

The current workaround is to use write.table(row.names = FALSE). But I would suggest to have row.names option in fread for flexibility.

DT = data.table(x=rep(c("a","b","c"),each=3), y=c(1,3,6), v=1:9)
write.table(DT, file = "test_writeDT.tsv", sep = "\t")
> fread(input="test_writeDT.tsv") #header is lost
   V1 V2 V3 V4
1:  1  a  1  1
2:  2  a  3  2
3:  3  a  6  3
4:  4  b  1  4
5:  5  b  3  5
6:  6  b  6  6
7:  7  c  1  7
8:  8  c  3  8
9:  9  c  6  9
> fread(input="test_writeDT.tsv", header=TRUE) # Wrong header
   1 a 1 1
1: 2 a 3 2
2: 3 a 6 3
3: 4 b 1 4
4: 5 b 3 5
5: 6 b 6 6
6: 7 c 1 7
7: 8 c 3 8
8: 9 c 6 9
> fread(input="test_writeDT.tsv", drop = 1, header=TRUE) # Looks about right, but still wrong header
   a 1 1
1: a 3 2
2: a 6 3
3: b 1 4
4: b 3 5
5: b 6 6
6: c 1 7
7: c 3 8
8: c 6 9
> read.table("test_writeDT.tsv") # Correct
  x y v
1 a 1 1
2 a 3 2
3 a 6 3
4 b 1 4
5 b 3 5
6 b 6 6
7 c 1 7
8 c 3 8
9 c 6 9

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions