Inspired by Prevent row names to be written to file when using write.csv, I am curious if there a way to ignore the index column in R using the read.csv()
formula. I want to import a text file into an RMarkdown document and don't want the row numbers to show in my HTML file produced by RMarkdown.
Running the following code
write.csv(head(cars), "cars.csv", row.names=FALSE)
produces a CSV that looks like this:
speed dist
4 2
4 10
7 4
7 22
8 16
9 10
But, if you read this index-less file back into R (ie, read.csv("cars.csv")
), the index column returns:
. speed dist
1 4 2
2 4 10
3 7 4
4 7 22
5 8 16
6 9 10
I was hoping the solution would be as easy as including row.names=FALSE
to the read.csv()
statement, as is done with write.csv()
, however after I run read.csv("cars.csv", row.names=FALSE)
, R gets sassy and returns an "invalid 'row.names' specification" error message.
I tried read.csv("cars.csv")[-1]
, but that just dropped the speed column, not the index column.
How do I prevent the row index from being imported?