view raw
O.rka O.rka - 8 months ago 43
R Question

How to set the row index name in R? (Like in Pandas)

How can I set the row index name in a


I tried looking on stackoverflow for the answer but I couldn't figure out how to even search for it?

This one kind of explains it but they are converting it to a matrix?
How do I name the "row names" column in r

> dimnames(DF_c) = c("sample","cluster")
Error in `dimnames<`(`*tmp*`, value = c("sample", "cluster" :
invalid 'dimnames' given for data frame

In Python
, I would simply do = "samples"
but I don't know how to do it in
. I noticed that when I saved it
write.table(DF_c, "output.tsv", sep="\t")
It puts my column label as the row name but I can't do something like
colnames(DF_c) = c( "samples","cluster")
since there is only 1 column?

# Clusters
DF_c = data.frame(last_iter$c)
rownames(DF_c) = row_labels
colnames(DF_c) = c( "cluster")

How to not include the
when it's writing table to output?

enter image description here


You are setting the row names and column names correctly, you just missed a piece from your 'write.table' command to remove the quotes:

write.table(DF_c, "output.tsv", sep="\t", quote = FALSE)

The reason that your column name goes over the row name in your output table seems to be a weirdness of R, you can get around it by creating a column for your row names with a column label, then writing the table out:

DF_c = data.frame(last_iter$c)
colnames(DF_c) = c( "cluster")
DF_c$rownames = row_labels

write.table(DF_c, "output.tsv", sep="\t", quote = FALSE)