Neptune
Neptune

Reputation: 627

R: Read csv with row and column name

I have a csv with column name in first row of the file, and column name in first column of the file, like this:

ColName1 ColName2 ... ColNameN
RowName1 ..
RowName2 ..
RowNameN .. 

If I use this command: read.csv("/Users/MNeptune/Documents/workspace R/simulatedProfiles.csv", header=TRUE)

I read correctly only columns name but not row name. What i can do to read row name?

Upvotes: 14

Views: 50377

Answers (3)

Sometimes you may need to convert a csv file as a matrix. This can be obtained by using as.matrix command. But if you need to have the header as column names and the first-row names as row names, then prepare your file with one blank space at the start of the first row. As an example,

    ,x,y
Anbu,12,89
Arivu,15,32
Siva,23,17

Then first issue command

data<-read.csv("file.csv", header=TRUE, row.names=1)

Then issue command

datam<-as.matrix(data)

you will get a matrix object with row and column names as found in the csv file.

Upvotes: 0

Chris
Chris

Reputation: 1278

Try read.csv("filename.csv", row.names = 1, header= TRUE).

Upvotes: 30

asb
asb

Reputation: 4432

There is a row.names option to read.csv (inherited from read.table) in which you can specify the column in the file to be used as row.names.

read.csv("/path/to/file", header=TRUE, row.names=1)

Upvotes: 7

Related Questions