1

I want to read a .csv file in R, in order to draw a heat map. My .csv data looks like:

Label|AA|BB|CC|DD
YE1  |0.1|0.9|0.3|0.7|
YE2  |0.3|0.5|0.2|0.3|
NE1  |0.9|0.6|0.1|0.4|
NE2  |0.8|0.7|0.1|0.5|

AA to DD are features and Label represent the row names. I want to plot heat map of features vs Labels

In order to read a .csv file, I wrote the following:

data <- read.csv("C:/example.csv", row.names= 1)

But, this gives me an error.

Error in read.table(file = file, header = header, sep = sep, quote = quote, : duplicate 'row.names' are not allowed

I came across reading a csv file with repeated row names in R where the answer says set row.names = NULL. But I don't want to set row.names = NULL. I want to read the row names in the data frame. How to do this?

Thanks.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
user3289492
  • 53
  • 2
  • 5
  • 1
    Your file doesn’t *have* row names. It has five named columns. Incidentally, there’s a broad trend in R to consider row names as bad, and not to use them (because they’re proper part of a table, and should be treated as such!). I recommend doing the same. – Konrad Rudolph Nov 20 '19 at 14:10

1 Answers1

2

The only way that I've found to do this is:

data <- read.csv("C:/example.csv")
rownames(data)=data[,1]
data=data[,-1]
which_command
  • 501
  • 1
  • 4
  • 15