200

Commands:

t <- data.frame(v = 5:1, v2 = 9:5)
write.csv(t, "t.csv")

Resulting file:

# "","v","v2"
# "1",5,9
# "2",4,8
# "3",3,7
# "4",2,6
# "5",1,5

How do I prevent first column with row index from being written to the file?

Henrik
68k15 gold badges153 silver badges166 bronze badges
asked Sep 20, 2011 at 11:24

2 Answers 2

366
write.csv(t, "t.csv", row.names=FALSE)

From ?write.csv:

row.names: either a logical value indicating whether the row names of
 ‘x’ are to be written along with ‘x’, or a character vector
 of row names to be written.
answered Sep 20, 2011 at 11:26
Sign up to request clarification or add additional context in comments.

4 Comments

I am ashamed because I did try ?write.csv but... Thx aix!
Yeah, the trick is to understand that this column represents row names.
Maybe one should rename this.
Would there be a way to make it that row.names = FALSE is the default? It is always frustrating in complex pipelines when a single script used the TRUE default and always causes a headache...
13

For completeness, write_csv() from the readr package is faster and never writes row names

# install.packages('readr', dependencies = TRUE)
library(readr)
write_csv(t, "t.csv")

If you need to write big data out, use fwrite() from the data.table package. It's much faster than both write.csv and write_csv

# install.packages('data.table')
library(data.table)
fwrite(t, "t.csv")

Below is a benchmark that Edouard published on his site

microbenchmark(write.csv(data, "baseR_file.csv", row.names = F),
 write_csv(data, "readr_file.csv"),
 fwrite(data, "datatable_file.csv"),
 times = 10, unit = "s")
## Unit: seconds
## expr min lq mean median uq max neval
## write.csv(data, "baseR_file.csv", row.names = F) 13.8066424 13.8248250 13.9118324 13.8776993 13.9269675 14.3241311 10
## write_csv(data, "readr_file.csv") 3.6742610 3.7999409 3.8572456 3.8690681 3.8991995 4.0637453 10
## fwrite(data, "datatable_file.csv") 0.3976728 0.4014872 0.4097876 0.4061506 0.4159007 0.4355469 10
answered Mar 9, 2019 at 5:50

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.