Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

name Column "rownames" in output file

Tags:

r

When I write a table in R, the rownames are printed as first column. However, the first column name (from the object) is automatically placed above the rownames, like this:

                 Object                           Output table:
       Sample1   Sample2   Sample3      Sample1  Sample2  Sample3:
Gene1     4        32         5           Gene1     4       32         5
Gene2     23       43         23          Gene2     3       43         23
Gene3     24       34         14          Gene3     4       34         14

I tried different ways to solve this, e.g.:

names <- c("GeneNr", names(DF))
names
# [1] "GeneNr"        "Sample1"      "Sample2"  "Sample3"
write.table(DF, file= "DF.txt"), sep = "\t", col.names = names)

but this does not work.

Does any know how to solve this?

like image 989
MtH Avatar asked Dec 11 '25 12:12

MtH


1 Answers

The simultaneous use of row.names=T and col.names=NA permits to correctly place your column names.

write.table(df, "df.txt", sep = "\t", row.names = T, quote = F, col.names = NA)
like image 183
Dhawal Kapil Avatar answered Dec 14 '25 05:12

Dhawal Kapil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!