Skip to content

Row names and read.table / read.csv

July 20, 2012

You’d think that I should know this stuff by now, but I often find myself re-learning things about reading data into R, so I’m writing down what I just re-learned: how to read data in with row names. The thing that frustrated me this time was that my csv file had a blank (i.e. empty quotes, "") entry at the top of the first column (i.e. the column with the row names). If there was no blank entry, and the first row simply had one fewer entry, then read.csv would have found my row names easily. But since there was a blank entry, read.csv got confused and put the row names into the first column of the resulting data frame.

Here’s the tip: Add row.names = 1 to the argument list of read.csv. The reason this works is that read.csv is being told that the first column (hence the row.names equals 1) contains the row names.

Advertisements
3 Comments leave one →
  1. Luiz Felipe Freitas permalink
    March 4, 2013 8:58 pm

    Thank you very much!

  2. Debjit permalink
    April 23, 2014 11:36 pm

    Really helped a lot, thanks Steven

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: