Koundy
Koundy

Reputation: 5503

How to indicate row.names=1 using fread() in data.table?

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.

Upvotes: 25

Views: 20894

Answers (4)

ikwee
ikwee

Reputation: 127

X <- as.matrix(fread("bigmatrix.csv"),rownames=1)

Upvotes: 6

qian sheng
qian sheng

Reputation: 1

Just one function, convert to a dataframe

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

Upvotes: 0

lolow
lolow

Reputation: 11

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")

Upvotes: 1

DJV
DJV

Reputation: 4863

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.

Upvotes: 0

Related Questions