2

in R I have in mydata data frame which column names is gene id and row names are sample id:

             ENSG00000000003   ENSG00000000004  ENSG00000000005
TCGA-QQ-A5VB       0.1               0.2              0.3
TCGA-DX-A8BO       0.3               0.1              0.2
TCGA-MO-A47R       0.3               0.1              0.2

Now I want to have a sample_ID column that valus are rownames(mydata) and add a column x before sample_ID column. also I want to insert rownames just by numbering same as below:

     x       Sample_ID    ENSG00000000003   ENSG00000000004  ENSG00000000005
1    1      TCGA-QQ-A5VB        0.1               0.2              0.3 
2    2      TCGA-DX-A8BO        0.3               0.1              0.2
3    3      TCGA-MO-A47R        0.3               0.1              0.2

I have to say mydata file has 56000 genes as colnames and the position of columns x and sample_ID are important. I don't want to have these columns in the end of columns.

I appreciate if anybody share his/her comment as code with me.

Best Regards,

Mohammad

neilfws
  • 32,751
  • 5
  • 50
  • 63
Mohammad
  • 103
  • 6

1 Answers1

1

Certainly a duplicated issue: Convert row names in multiple data frames to column in data frame

I hope this helps:

data <- data.frame(x = 1:nrow(data), Sample_ID = row.names(data), data)
Ismail Müller
  • 395
  • 1
  • 7
  • Hi, Thanks.one more question. if I want add these 2 column in end of mydata columns. how can I do? – Mohammad Mar 08 '19 at 05:45
  • Hi. You just put them attention the end of the Data.frame call `data.frame(data, x = 1:nrow(data), Sample_ID = row.names(data))` – Ismail Müller Mar 08 '19 at 06:52