23

I want to consider the first column in my .csv file as a sequence of rownames. Usually I used to do the following:

read.csv("example_file.csv", row.names=1)

But I want to do this with the fread() function in the data.table R package, as it runs very quickly.

rwp
  • 1,786
  • 2
  • 17
  • 28
Koundy
  • 5,265
  • 3
  • 24
  • 37
  • 4
    Don't use rownames with data.table. Just have them as a column. – Roland Jun 26 '14 at 07:51
  • 8
    If you want to convert to a `data.frame` after `fread`, just use `data.frame(fread("example_file.csv"), row.names=1)`. But I suggest working with `data.table` instead. – shadow Jun 26 '14 at 07:55
  • But my remaining columns are numeric, and i want to apply some functions on them. I want these to be rownames only and intend to work with data.table not data.frame that's what the problem is – Koundy Jun 26 '14 at 08:34
  • @koundy To do that see http://stackoverflow.com/a/16783769/403310 – Matt Dowle Jun 26 '14 at 11:17
  • 3
    @MattDowle I think there is some validity here, if one wanted to use the resulting object as a matrix, rather than a data.frame/data.table. I am working with some data that I want to apply matrix math to, and would prefer to use fread for speed purposes. Currently I am using `data.matrix(read.csv("myfile.csv", row.names = 1))`. – dayne Oct 10 '14 at 16:28
  • 1
    @dayne Is [`bigmemory::read.big.matrix`](http://www.rdocumentation.org/packages/bigmemory/functions/write.big.matrix) faster than `read.csv` and fast enough? If not please file a feature request on the github tracker - thanks. – Matt Dowle Oct 10 '14 at 20:34
  • Check out `mstrsplit` from the "iotools" package for another fast option. Not sure how it compares with `fread`.... – A5C1D2H2I1M1N2O1R2T1 Jan 11 '18 at 03:12

4 Answers4

5
X <- as.matrix(fread("bigmatrix.csv"),rownames=1)
ikwee
  • 119
  • 2
  • 3
1

Why not saving the rownames in a column:

df <- data.frame(x=rnorm(1000))
df$row_name = row.names(df)   
fwrite(df,file="example_file.csv")

Then you can load the saved CSV.

df <- fread(file="example_file.csv")
Feng Tian
  • 1,559
  • 1
  • 18
  • 27
lolow
  • 11
  • 2
0

From a small search I've done, data.tables never uses row names. Since data.tables inherit from data.frames, it still has the row names attribute. But it never uses them.

However, you can probably use this answer (similar post) and later make the rowname column into your actual rownames. Though, it might not be efficient.

DJV
  • 4,743
  • 3
  • 19
  • 34
0

Just one function, convert to a dataframe

a <- fread(file="example_file.csv")  %>% as.data.frame()
row.names(a) <- a$V1